I have an array of arrays formatted like the following:
$list = [ ["fdsa","1","fdsa"],["sadf","0","asdf"],["frfrf","0","sadfdsf"] ]
How can I alphabetize $list based on the first value of every inner array?
Thanks!
I have an array of arrays formatted like the following:
$list = [ ["fdsa","1","fdsa"],["sadf","0","asdf"],["frfrf","0","sadfdsf"] ]
How can I alphabetize $list based on the first value of every inner array?
Thanks!
I had this function for another answer but it can be modded to do the same:
// This sorts simply by alphabetic order
function reindex( $a, $b )
{
// Here we grab the values of the 'code' keys from within the array.
$val1 = $a[0];
$val2 = $b[0];
// Compare string alphabetically
if( $val1 > $val2 ) {
return 1;
} elseif( $val1 < $val2 ) {
return -1;
} else {
return 0;
}
}
// Call it like this:
usort( $array, 'reindex' );
print_r( $array );
Original: Sorting multidimensional array based on the order of plain array
function reindex($a,$b){ return strcmp($a[0],$b[0]);} (which does the same thing).asort($list);
This will simply do the job for you.