Array ( [text] => tests [language] => [advertiserC...">

PHP multidimensional array (usort)

I have an associative array like this

Array ( ["News 1"] => Array ( [text] => tests [language] => [advertiserCompetitionScale] => 5 [avgSearchVolume] => 7480000 [lastMonthSearchVolume] => 9140000 ) ["News 2"] => Array ( [text] => personality tests [language] => [advertiserCompetitionScale] => 5 [avgSearchVolume] => 165000 [lastMonthSearchVolume] => 201000 ) ["News 3] => Array ( [text] => online tests [language] => [advertiserCompetitionScale] => 5 [avgSearchVolume] => 246000 [lastMonthSearchVolume] => 301000 ) ) 

I managed to sort it by the column I want (e.g. LastMonthSearchVolume)

 // compare function function cmpi($a, $b) { return strcmp($a["lastMonthSearchVolume"], $b["lastMonthSearchVolume"]); } // do the array sorting usort($latest_array, 'cmpi'); 

The problem is when I dump the array to see the result that usort violated my associative array by deleting "News 1", "News 2", etc. and replacing it with 0,1,2 ...

Is there any sorting solution to keep the column name?

thanks

+4
source share
2 answers

Instead of usort use the uasort function, which preserves index association.

+4
source

Use uasort . usort does not support associative keys, but uasort does.

+1
source

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


All Articles