我有一个包含多个数组的数组和一个包含1 2 3 4 5 6 7 8等顺序的值....
我无法改变这种结构
["Categ1"]=>
array(3) {
["order"]=>
string(1) "5"
[0]=>
array(5) {
["content"]=>
string(x) "xxx"
["content"]=>
string(x) "xxx"
["content"]=>
string(x) "xxx"
["content"]=>
string(x) "xxx"
["content"]=>
NULL
}
[1]=>
array(5) {
["content"]=>
string(x) "xxx"
["content"]=>
string(x) "xxx"
["content"]=>
string(x) "xxx"
["content"]=>
string(x) "xxx"
["content"]=>
NULL
}
}
["Categ2"]=>
array(3) {
["order"]=>
string(1) "2"
[0]=>
array(5) {
["content"]=>
string(x) "xxx"
["content"]=>
string(x) "xxx"
["content"]=>
string(x) "xxx"
["content"]=>
string(x) "xxx"
["content"]=>
NULL
}
[1]=>
array(5) {
["content"]=>
string(x) "xxx"
["content"]=>
string(x) "xxx"
["content"]=>
string(x) "xxx"
["content"]=>
string(x) "xxx"
["content"]=>
NULL
}
}
我需要的是非常简单但我无法找到解决方案:
["Categ2"]=>
array(3) {
["order"]=>
string(1) "2"
[0]=>
array(5) {
["content"]=>
string(x) "xxx"
["content"]=>
string(x) "xxx"
["content"]=>
string(x) "xxx"
["content"]=>
string(x) "xxx"
["content"]=>
NULL
}
[1]=>
array(5) {
["content"]=>
string(x) "xxx"
["content"]=>
string(x) "xxx"
["content"]=>
string(x) "xxx"
["content"]=>
string(x) "xxx"
["content"]=>
NULL
}
}
["Categ1"]=>
array(3) {
["order"]=>
string(1) "5"
[0]=>
array(5) {
["content"]=>
string(x) "xxx"
["content"]=>
string(x) "xxx"
["content"]=>
string(x) "xxx"
["content"]=>
string(x) "xxx"
["content"]=>
NULL
}
[1]=>
array(5) {
["content"]=>
string(x) "xxx"
["content"]=>
string(x) "xxx"
["content"]=>
string(x) "xxx"
["content"]=>
string(x) "xxx"
["content"]=>
NULL
}
}
只是订单价值
的简单订单我还没有在stackoverflow上找到解决方案
答案 0 :(得分:1)
您需要定义自定义排序功能并将其交给php排序功能。例如uasort
这个简约的例子表明它非常好。
$cats = [
'cat1' => [
"order" => 5,
["foo" => "bar"]
],
'cat2' => [
"order" => 3,
["foo" => "bar"]
]
];
uasort($cats, function($a, $b){
return $a['order'] > $b['order'];
});
var_dump($cats);
将输出:
array (size=2)
'cat2' =>
array (size=2)
'order' => int 3
0 =>
array (size=1)
'foo' => string 'bar' (length=3)
'cat1' =>
array (size=2)
'order' => int 5
0 =>
array (size=1)
'foo' => string 'bar' (length=3)