多维数组按DESC 顺序重新排列,内部多维数组在DESC中,但外部父数组不起作用



工作子数组在这里与父多维数组在一起。这里 子数组按 DESC 顺序运行。

[
{
"variation_id": "23",
"variation_price": "200.00"
},
{
"variation_id": "25",
"variation_price": "100.00"
}
],
[
{
"variation_id": "31",
"variation_price": "150.00"
},
{
"variation_id": "30",
"variation_price": "80.00"
}
],
[
{
"variation_id": "45",
"variation_price": "300.00"
},
{
"variation_id": "44",
"variation_price": "200.56"
}
]

并且想按照这样的顺序工作,谢谢意味着整个多维(父数组和子数组(按 DESC 顺序工作。

[
[
{
"variation_id": "45",
"variation_price": "300.00"
},
{
"variation_id": "44",
"variation_price": "200.56"
}
],
[
{
"variation_id": "23",
"variation_price": "200.00"
},
{
"variation_id": "25",
"variation_price": "100.00"
}
],
[
{
"variation_id": "31",
"variation_price": "150.00"
},
{
"variation_id": "30",
"variation_price": "80.00"
}
]
]

我已经为第一个编写了查询,以将子数组作为 DESC 顺序获取查询 -> 的结果

$response  = $find_variation_array;
$sort = array();
foreach($response as $k=>$v) {
$sort['variation_price'][$k] = $v['variation_price'];
}
array_multisort($sort['variation_price'], SORT_DESC, $response);

不适用于第二步,即父数组。
任何人都可以分享我如何以 DESC 顺序获取整个父级和子多维数组的任何想法。提前感谢您的知识分享。

假设你的数组是这样的:

[ 
[
[
"variation_id" => "23",
"variation_price" => "200.00"
],
[
"variation_id" => "25",
"variation_price" => "100.00"
]
],
[
[
"variation_id" => "31",
"variation_price" => "150.00"
],
[
"variation_id" => "30",
"variation_price" => "80.00"
]
],
[
[
"variation_id" => "45",
"variation_price" => "300.00"
],
[
"variation_id" => "44",
"variation_price" => "200.56"
]
]
]

那么这可能是一种解决的方法:

$test_array = 
[ 
[
[
"variation_id" => "23",
"variation_price" => "200.00"
],
[
"variation_id" => "25",
"variation_price" => "100.00"
]
],
[
[
"variation_id" => "31",
"variation_price" => "150.00"
],
[
"variation_id" => "30",
"variation_price" => "80.00"
]
],
[
[
"variation_id" => "45",
"variation_price" => "300.00"
],
[
"variation_id" => "44",
"variation_price" => "200.56"
]
]
];

$sort_array_order = array();
foreach ($test_array as $test_key => $test_value) {
$temp_sumation_of_variation_price = 0;
foreach ($test_value as $key => $value) {
$temp_sumation_of_variation_price += $value['variation_price'];
}
$sort_array_order[] = $temp_sumation_of_variation_price;
}
array_multisort($sort_array_order, SORT_DESC, $test_array);
echo "<pre>";print_r($test_array);echo "</pre>";

相关内容

  • 没有找到相关文章

最新更新