我喜欢使用PHP按升序排序以下JSON数据的值< strong> salary 。
{
"allresults": [
{
"results": [
{
"Name": "Peter",
"salary": 1000
}
]
},
{
"results": [
{
"Name": "Riya",
"salary": 999
}
]
}
]
}
必填结果:
答案 0 :(得分:3)
您可以使用uasort编写自定义排序功能。这样的事情对你有用:
$data = json_decode($json, true);
uasort($data['allresults'], function($a, $b) {
$aSalary = $a['results'][0]['salary'];
$bSalary = $b['results'][0]['salary'];
return $aSalary - $bSalary;
});
foreach($data['allresults'] as $item) {
printf("%s: %d<br />", $item['results'][0]['Name'], $item['results'][0]['salary']);
}
答案 1 :(得分:0)
玩这个:
$jsonIterator = new RecursiveIteratorIterator(
new RecursiveArrayIterator(your_json($file, TRUE)),
RecursiveIteratorIterator::SELF_FIRST
);
foreach ($jsonIterator as $key => $val) {
//your control
}
答案 2 :(得分:0)
第一个json到php数组 - &gt; $ json_arr = json_decode($ json,true);
之后 - &gt;您可以使用 array_multisort http://php.net//manual/en/function.array-multisort.php