我有一个如下所示的数组:
"lines":[
{"value":1,"id":0},
{"value":10,"id":1}
]
我的目标是始终获得一个有价值的:第一个10,所以我想用ksort排序,但我不确定
这是我的目标:
"lines":[
{"value":10,"id":1},
{"value":1,"id":0}
]
任何可以帮助的人都非常感谢!
答案 0 :(得分:0)
@Frank Lucas如果你想按任意顺序排列多维数组 键,所以你可以使用usort()函数
如果你想按"值"对数组进行排序索引按降序排列,所以请尝试下面的代码并理解它
<?php
$lines = array(array("value"=>1,"id"=>0), array("value"=>10,"id"=>1));
function order($a, $b){
if ($a["value"] == $b["value"]) { //here pass your index name
return 0;
}
return ($a["value"] > $b["value"]) ? -1 : 1; // this your key "value" will be in descending order as requirement
//but if you want to change this order from descending to Ascending order so just change the -1 and 1 place in above condition
}
usort($lines,"order");
print_r($lines);
?>
它将解决您的问题(y)