对多维数组进行排序但不重新分配顶级键
数组
$tagHolder[$row['id']] = array(
"name" => $row['name'],
"primary" => $row['primary'],
"child" => $row['child'],
"order" => $row['order']
);
usort 函数
function sortAsc($x, $y){
if ( $x['order'] == $y['order'] )
return 0;
else if ( $x['order'] < $y['order'] )
return -1;
else
return 1;
}
将按 'order' 排序,但不会保留原始 $row['id']
键,而是将第一个位置重新分配为 0 等。如何使排序函数排序但保持 $row['key']
不变?
array
$tagHolder[$row['id']] = array(
"name" => $row['name'],
"primary" => $row['primary'],
"child" => $row['child'],
"order" => $row['order']
);
usort function
function sortAsc($x, $y){
if ( $x['order'] == $y['order'] )
return 0;
else if ( $x['order'] < $y['order'] )
return -1;
else
return 1;
}
Will order by 'order' BUT will not keep the original $row['id']
keys, instead it reassigns the first prosition as 0 and so on. How can I make the sort function sort but keep the $row['key']
untouched?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
uasort
而不是usort
保持键关联。Use
uasort
instead ofusort
to keep the key association.