PHP - 如何通过其中一个数组值对完整数组进行排序?

时间:2016-08-03 14:06:51

标签: php sorting

如何根据$rr中的升序排序整个$rr[xx]->label

$result=curl_exec ($ch);
$rr = json_decode($result);    
foreach($rr as $k=>$v) {
  echo $v->label . ' - ' . $v->id .  '<Br/>';
}

输出:

Oman - 500
Uk - 99
America - 100
Denmark - 120

预期输出(按标签排序):

America - 100
Denmark - 120
Oman - 500
Uk - 99

1 个答案:

答案 0 :(得分:2)

<?php
  $arr= array("Oman"=>"500","uk"=>"99","America "=>"100","Denmark "=>"120");
  ksort($arr);

  foreach($arr as $x => $x_value) {
     echo "Key=" . $x . ", Value=" . $x_value;
     echo "<br>";
  }
?>

使用ksort - sorts an associative array in ascending order, according to the value