echo '<pre>'.print_r($listings,1).'</pre>';
ksort($listings, SORT_NUMERIC);
echo '<pre>'.print_r($listings,1).'</pre>';
输出:
Array
(
[quick-brown-fox] => Array
(
[0] => Quick-brown-fox
[1] => quick-brown-fox
[4] => general_thumbs/quick-brown-fox.jpg
[2] => 320
[3] => 240
)
)
Array
(
[quick-brown-fox] => Array
(
[0] => Quick-brown-fox
[1] => quick-brown-fox
[4] => general_thumbs/quick-brown-fox.jpg
[2] => 320
[3] => 240
)
)
我尝试了foreach
,但它不会影响原始数组,并且for
不起作用,因为它是键,而不是索引。在这种情况下,我该怎么办?
您在此$listings
数组中嵌套了数组。要对其进行排序,请像这样写:
foreach($listings as $k => $a){
ksort($a, SORT_NUMERIC);
$listings[$k] = $a;
}
array_walk(
$listings,
function(&$value) {
ksort($value, SORT_NUMERIC);
}
);