php sort assocative
I'm trying to sort my array and I keep getting a result of 1
here is the code pleas help
$foo = array(
2 => "Sports",
40 => "Parent and Families",
43 => "Arts and Enter开发者_高级运维tainment",
);
$foo = sort($foo);
I'd like them to be sorted by value
Sort doesn't return the sorted array. It returns TRUE of FALSE on success. The array is passed by reference. So call the method and just use it
$foo = array(
2 => "Sports",
40 => "Parent and Families",
43 => "Arts and Entertainment",
);
sort($foo); //foo is now sorted
EDIT
Note however, that sort() actually reassigns your indices. You should use asort()
instead of sort if you want to keep associations
If you need to mainatin index association, use asort(array &$array [, int $sort_flags = SORT_REGULAR]). Note the pass by reference on $array ( --> check manual what the function outputs).
$foo = array(
2 => "Sports",
40 => "Parent and Families",
43 => "Arts and Entertainment",
);
asort($foo);
print_r($foo);
prints
Array
(
[43] => Arts and Entertainment
[40] => Parent and Families
[2] => Sports
)
you can use asort()
http://php.net/manual/en/function.asort.php
精彩评论