Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get first character from array in php

Tags:

arrays

php

  • I need to create brand for an eCommerce site.
  • I need a solution that gives me first character from an array.
  • I tried some code but it was not so useful.
  • I need brand like I mentioned bellow.

like

A             
Alpha
Aloo
Amakeaviral

B
Boki
Bone

my data coming from database, in a array. I need first character from array.

I tried:

$my_array = array("Alpha", "Aloo", "Amakeaviral", "Boki", "Bone");

for($i = 0; $i <= count($my_array); $i++){
    $first_char = $my_array[$i]{0}; 
    echo $first_char;
}

but this not working well. How can I do this?

like image 976
Deepak Kumar Avatar asked Sep 16 '14 06:09

Deepak Kumar


4 Answers

Try with substr() as simply short

$my_array = array("Alpha", "Aloo", "Amakeaviral", "Boki", "Bone");
foreach($my_array as $v){
    echo substr($v, 0, 1);
}

or your code:- remove = from loop <=(else you will get notices) and use [] rather {}

$my_array = array("Alpha", "Aloo", "Amakeaviral", "Boki", "Bone");
for($i=0; $i < count($my_array); $i++){
    $first_char = $my_array[$i][0]; 
    echo $first_char;
}
like image 196
Rakesh Sharma Avatar answered Oct 09 '22 23:10

Rakesh Sharma


hope this will work for you -

$my_array = array("Alpha","Aloo","Amakeaviral","Boki","Bone");
$newArray = array();
foreach($my_array as $value) {
   $first_char = $value[0];
   if (!empty($newArray)) {
       $flag = false;
       foreach ($newArray as $key => $val) {
           if ($first_char == $key){
                $newArray[$key][] = $value;
                $flag = true;
           }
       }
       if (!$flag) {
           $newArray[$first_char][] = $first_char;
           $newArray[$first_char][] = $value;
       }
   } else {
        $newArray[$first_char][] = $first_char;
        $newArray[$first_char][] = $value;
   }
}
var_dump($newArray);

Same as above but shortened code:

$my_array = array("Alpha","Aloo","Amakeaviral","Boki","Bone");
$newArray = array();
foreach($my_array as $value) {
    if (empty($newArray[$value[0]])){
        $newArray[$value[0]][]=$value[0];
    }
    $newArray[$value[0]][] = $value;
}
var_dump($newArray);
like image 41
Sougata Bose Avatar answered Oct 09 '22 23:10

Sougata Bose


substr

Finding first character

substr("Hello", 0, 1); //output "H"

Try:

$first_char = substr($my_array[$i], 0, 1);

Full code:

for($i = 0; $i < count($my_array); $i++){
    echo $first_char = substr($my_array[$i], 0, 1); 
}

Note: $i <= count($my_array) should be $i < count($my_array)

Live DEMO

like image 3
Manwal Avatar answered Oct 10 '22 01:10

Manwal


Basically, every string is an array and can be accessed like it:

$str = 'This is a string';
echo $str[0]; // Output: T
echo $str[1]; // Output: h
echo $str[2]; // Output: i

Just change this:

$first_char = $my_array[$i]{0}; 

into this:

$first_char = $my_array[$i][0]; 
like image 2
toesslab Avatar answered Oct 10 '22 01:10

toesslab