我有一个像这样的数组
array:2 [▼
"folder1" => array:3 [▼
1 => "d1_2.png"
2 => "d1_1.png"
3 => "d1_3.png"
]
"folder2" => array:3 [▼
0 => "d2_2.png"
1 => "d2_3.png"
3 => "d2_1.png"
]
]
我想做的是根据值对这个数组进行排序。所以输出应该是这样的
array:2 [▼
"folder1" => array:3 [▼
0 => "d1_1.png"
1 => "d1_2.png"
2 => "d1_3.png"
]
"folder2" => array:3 [▼
0 => "d1_1.png"
1 => "d1_2.png"
2 => "d1_3.png"
]
]
我见过的所有示例都是根据键值排序的,但我没有键。我尝试了几种排序函数,但似乎都没有对数组进行排序。
如何根据我拥有的数组对其进行排序?
您可以使用此功能:
asort() - sort associative arrays in ascending order, according to the value
使用
array_walk
(foreach 的功能方式)遍历外部数组,然后在其中使用 sort
http://php.net/manual/en/function.array-walk.php
http://php.net/manual/en/function.sort.php
$arr = array(
"folder1" => array(
"1" => "d1_2",
"2" => "d1_1",
"3" => "d1_3"
),
"folder2" => array(
"1" => "d2_3",
"2" => "d2_1",
"3" => "d2_2"
)
);
var_dump($arr);
array_walk($arr, function (&$e) {
sort($e);
});
var_dump($arr);