用键创建一个新的数组


Make a new array with keys

我在一个变量中有这样的数据:

array(2) {
  ["fields"]=> 
      array(2){
           ...
               }
  ["price"] => int(36)
}
array(2) {
  ["fields"]=> 
      array(2){
           ...
               }
  ["price"] => int(25)
}
....

我需要按价格对这些数据进行排序。我可以使用usort()函数,但我必须使用一个包含键和值的数组。如何从这些数据中创建一个数组?

我尝试使用array_fill(),但结果只是第一个元素。

$item = Array("fields" => $arFields, "price" => $price_int); // this is data that I need to sort
$item1 = array_fill(0,15,$item);

Sort I do like this

function pricesort($a, $b) 
{
    if ($a["price"] == $b["price"]) {
        return 0;
    }
    return ($a["price"] < $b["price"]) ? -1 : 1;
}
$sort = uasort($item, "pricesort");

我尝试了array_multisort,但它只对第一个值

产生相同的结果

试试这个:

foreach ($Your_array as $key => $row) {
    $price[$key]  = $row["price"]; 
}
array_multisort($price, SORT_DESC, $Your_array);

如果要按升序排序,则将SORT_DESC更改为SORT_ASC