Sort Mulitdemensional Array PHP

I have the following array, it is currently created, sorted by entity_count (output by a query executed in cakephp - I only need the best entities), now I want to sort the array for Entity-> title.

I tried to do this with array_multisort , but could not. Is it possible?

 Array ( [0] => Array ( [Entity] => Array ( [title] => Orange ) [0] => Array ( [entitycount] => 76 ) ) [1] => Array ( [Entity] => Array ( [title] => Apple ) [0] => Array ( [entitycount] => 78 ) ) [2] => Array ( [Entity] => Array ( [title] => Lemon ) [0] => Array ( [entitycount] => 85 ) ) ) 
+1
source share
3 answers

Create a callback function as follows:

 function callback($value) { return isset($value['entity']['title']) ? $value['entity']['title'] : null; } 

Then run it a array_map and multi sort

 array_multisort(array_map($myArray,'callback'), $myArray); 
+3
source

Try the following:

 $keys = array_map($arr, function($val) { return $val['Entity']['title']; }); array_multisort($keys, $arr); 

Here array_map and an anonymous function (available since PHP 5.3, you can use create_function in previous versions) are used to get an array of headers, which is then used to sort the array according to their names.

0
source

You need to write a custom comparison function and then use usort . Call it using:

 usort ( $arrayy , callback $cmp_function ); 
0
source

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


All Articles