gpt4 book ai didi

php - array_multisort() : Array sizes are inconsistent

转载 作者:行者123 更新时间:2023-12-02 05:25:35 33 4
gpt4 key购买 nike

我正在尝试使用 array_multisort() 在其子数组的基础上对数组进行排序功能......

在尝试的同时;

print_r($mar); echo '<br>';
$arr2 = array_multisort($mar, array('wek'=>SORT_ASC));
print_r($arr2);

出现错误 array_multisort(): Array sizes are inconsistent
排序前的输出是
Array ( 
[0] => Array ( [dat] => 1 [wek] => 5 [mac] => A100 [mcr] => #00c8ff )
[1] => Array ( [dat] => 2 [wek] => 9 [mac] => A100 [mcr] => #00c8ff )
[2] => Array ( [dat] => 5 [wek] => 13 [mac] => A100 [mcr] => #00c8ff )
[3] => Array ( [dat] => 5 [wek] => 6 [mac] => A101 [mcr] => #ff8800 )
[4] => Array ( [dat] => 13 [wek] => 17 [mac] => A100 [mcr] => #00c8ff )
[5] => Array ( [dat] => 20 [wek] => 21 [mac] => A100 [mcr] => #00c8ff )
[6] => Array ( [dat] => 8 [wek] => 14 [mac] => A101 [mcr] => #ff8800 )
)

我需要的是:
Array ( 
[0] => Array ( [dat] => 1 [wek] => 5 [mac] => A100 [mcr] => #00c8ff )
[3] => Array ( [dat] => 5 [wek] => 6 [mac] => A101 [mcr] => #ff8800 )
[1] => Array ( [dat] => 2 [wek] => 9 [mac] => A100 [mcr] => #00c8ff )
[2] => Array ( [dat] => 5 [wek] => 13 [mac] => A100 [mcr] => #00c8ff )
[6] => Array ( [dat] => 8 [wek] => 14 [mac] => A101 [mcr] => #ff8800 )
[4] => Array ( [dat] => 13 [wek] => 17 [mac] => A100 [mcr] => #00c8ff )
[5] => Array ( [dat] => 20 [wek] => 21 [mac] => A100 [mcr] => #00c8ff )
)

最佳答案

下一行有错误:

$arr2 = array_multisort($mar, array('wek'=>SORT_ASC));

您试图将返回结果存储到数组中,但 array_multisort 返回 bool 值而不是排序数组:

这样做是为了对多维数组进行排序 $mar :
foreach ($mar as $key => $row)
{
$wek[$key] = $row['wek'];
}

// Sort the data with wek ascending order, add $mar as the last parameter, to sort by the common key

array_multisort($wek, SORT_ASC, $mar);

$mar 数组现在在上述操作后排序..

关于php - array_multisort() : Array sizes are inconsistent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22247844/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com