There is an array:

array(191) { [1]=> array(8) { ["a"]=> string(3) "3" ["b"]=> string(53) "4" ["c"]=> string(31) "8" } [2]=> array(8) { ["a"]=> string(3) "5" ["b"]=> string(44) "666" ["c"]=> string(35) "69" ["d"]=> string(10) "777" } } 

output the required cycle

 foreach ($addressarray as $arr) { echo " раз: ".$arr['a']."<br /> два: ".$arr['c']." <br /> три: ".$arr['d']; } 

How to sort an array by ["a"] values? That is, the output was:

 раз: 3 два: 4 три: 8 раз: 5 два: 666 три: 69 

About sort and asort read, but for some reason does not sort by nested array (

An example of the array being parsed:

 array(191) { [1]=> array(8) { ["streetAddress"]=> string(53) "г.Жуковка, ул.Карла Маркса" ["region_name"]=> string(31) "Брянская область" ["city_name"]=> string(14) "Жуковка" ["workhours"]=> string(55) "пн-сб" ["phone"]=> string(11) "89123456789" } [2]=> array(8) { ["streetAddress"]=> string(44) "г.Анапа, ул.Крымская" ["region_name"]=> string(35) "Краснодарский край" ["city_name"]=> string(10) "Анапа" ["workhours"]=> string(55) "пн-сб" } 

Here you need to sort by region_name. output addresses in alphabetical order

  • Try reading about array_multisort - it can also sort multidimensional arrays .. - And
  • Not a dump, but a code. Different things. I do not want to rewrite the array. - And

1 answer 1

You can sort using uasort () :

 $array = []; // Ваш массив 

PHP example> = 7:

 uasort($array, function($a, $b){ return $a["region_name"] <=> $b["region_name"]; }); 

PHP example> = 5:

 uasort($array, function($a, $b) { if ($a["region_name"] == $b["region_name"]) { return 0; } return ($a["region_name"] < $b["region_name"]) ? -1 : 1; }); 

View array after sorting:

 var_dump($array); 
  • syntax error, unexpected '>' it seems on the spaceship <=> swears. Version pxp 5.4. Alternatively can this somehow be compared? @ Edward - theblackpost
  • @theblackpost yes, <=> supported with php 7. Need an example for your version? Or do you find it on the link? ) - Edward
  • if you hint - honor to you and praise!) @ Edward - theblackpost
  • @theblackpost I added my answer. - Edward
  • Everything is like a clock! Thank you, @Eduard - theblackpost