PHP: single value in array according to alphabetical order

I want to find the values ​​in the array according to the alphabet and I want to make a list of the values ​​of the array according to the alphabet order.

my array looks like this:

Array ([0] => Array ([0] => Adidas [1] => AKG [2] => Apple [3] => Barrata [4] => Canon [5] => Dell [6] => Dixons [7] => HTC [8] => Liverpool [9] => Microsoft [10] => Pirelli Tires [11] =>)

)

and I want to make a list of values ​​according to the alphabet as follows:

A           
________        
  Adidas       
  AKG 

Plz any idea?

0
7

, , :

    $a = array("Chicken","Fish","Hello","Lebowski","What","hey","foo");

    asort($a);
    $alphaArr = array();

    foreach(range('A', 'Z') as $letter) {

        // create empty array at offset for current letter
        $alphaArr[$letter] = array();

        foreach($a as $item) {

            // if the first letter starts with current letter
            // push it into subarray
            if (substr(strtolower($item),0,1) == strtolower($letter)) {
                $alphaArr[$letter][] = $item;
            }
        }
    }
    print_r($alphaArr);
0

asort

asort($a);
foreach($a AS $v) {
    echo $v . "<br />";
}
+1

, , sort.

0

$fruits = array ( "", "", "", "" ); ($ ); reset ($ ); while (list ($ key, $val) = each ($ fruits)) {  echo "fruits [". $key. "] =". $val. "\n"; }

0

.

, .

, , .

0

, :

$sorted = array();
foreach($companies as $company) {
    $sorted[ strtoupper($company{0}) ][] = $company;
}
0

Just sort the array using a function sort()and then print it.
So that these letter signatures simply adjust the first letter, and then compare it with the previous one:

$old='';
foreach ($words as $name) {
  if ($name[0] != $old) {
    echo $name[0]."<hr>\n";
  }
  echo $name."<br>\n";
  $old=$name[0];
}
0
source

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


All Articles