ksort 似乎不起作用
ksort doesn't seem to work
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);
}
);
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);
}
);