Deepak Kumar
Deepak Kumar

Reputation: 91

how to get first character from array in php

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?

Upvotes: 6

Views: 12116

Answers (7)

Lee Wiggins
Lee Wiggins

Reputation: 351

I know this is a bit old I'm sorry but this is how I did it with an array of objects. I wanted to display the title of each object in an alphabetised page. I used the following:

$items = array
(
[0] => stdClass Object
    (
        [title] => Alpha
        [name] => Joe Blogs
        [address] => 123 Harry Street
    )

[1] => stdClass Object
    (
        [title] => Bravo
        [name] => Jane Doe
        [address] => 456 Upton Street
    )

[2] => stdClass Object
    (
        [title] => Charlie
        [name] => Jane Doe
        [address] => 456 Upton Street
    )

)

Then declared this function in a helper class

public static function alphaSortItems ($items) {
        $sortedItems = array();
        foreach ($items as $item) {
            $sortedItems[$item->title[0]][] = $item;
        }
        return $sortedItems;
    }

Then to display them I used

 <?php $sortedItems = Helper::alphaSortItems($this->items); ?>
        <?php foreach ($sortedItems as $key => $value) : ?>
            <h2><?php echo $key; ?></h2>
            <?php foreach ($value as $item) : ?>
                <h3><?php echo $item->title; ?></h3>
            <?php endforeach; ?>
        <?php endforeach; ?>

That's how I did it anyway :-)

Upvotes: 2

Sougata Bose
Sougata Bose

Reputation: 31739

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);

Upvotes: 3

Mahadeva Prasad
Mahadeva Prasad

Reputation: 709

Try this

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

Upvotes: 2

Manwal
Manwal

Reputation: 23816

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

Upvotes: 3

Rakesh Sharma
Rakesh Sharma

Reputation: 13728

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;
}

Upvotes: 11

Ritesh d joshi
Ritesh d joshi

Reputation: 823

Please try this:

$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;
}

Upvotes: 1

Daniel Gasser
Daniel Gasser

Reputation: 5133

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]; 

Upvotes: 2

Related Questions