How to get the first character from an array in php

  • I need to create a brand for an e-commerce site.
  • I need a solution that gives me the first character from an array.
  • I tried the code, but it was not so useful.
  • I need a brand, as I mentioned below.

like

A Alpha Aloo Amakeaviral B Boki Bone 

my data comes from a database in an array. I need the first character from the 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 it does not work well. How can i do this?

+5
source share
7 answers

hope this works 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); 
+2
source

Try with substr() just briefly

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

or your code: - remove = from the loop <= (otherwise you will receive notifications) 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; } 
+7
source

substr

Search for the 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) must be $i < count($my_array)

Live demo

+3
source

Basically, each row is an array and can be accessed as:

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

in it:

 $first_char = $my_array[$i][0]; 
+2
source

Please try the following:

 $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; } 
+1
source

try it

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

I know this is a bit outdated, I'm sorry, but I did this with an array of objects. I wanted to display the title of each object on the page with the alphabet. 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 ) ) 

This function is then declared 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 I did it anyway :-)

0
source

Source: https://habr.com/ru/post/1202649/


All Articles