私は、子の値に基づいて特定の順序で並べ替えたいデコードされたjsonデータをいくつか持っています。私は、以下のものを使用して問題なく値を出力してい子値による配列の並べ替え
Array
(
[location] => Array
(
[id] => 10215235726
[title] => demo title
[media] => Array
(
[nodes] => Array
(
[0] => Array
(
[id] => 15129696952092
[thumbnail_src] => thumb_15131.jpg
[is_video] => 1
[code] => BTg35sbvdfc
[date] => 1494577207
[display_src] => image_15131.jpg
[video_views] => 318
[caption] => Batman
[comments] => Array
(
[count] => 2
)
[likes] => Array
(
[count] => 87
)
)
[1] => Array
(
[comments_disabled] =>
[id] => 47484867964790738
[thumbnail_src] => thumb_11536.jpg
[is_video] =>
[code] => BTmSAQghufS
[date] => 1493745672
[display_src] => image_11536.jpg
[caption] => Aquaman
[comments] => Array
(
[count] => 2
)
[likes] => Array
(
[count] => 73
)
)
etc...
:
$json = json_decode(file_get_contents("http://www.linktojson.com"), true);
foreach($json['location']['media']['nodes'] as $value) {
echo $value['display_src'];
}
しかし、今、私は好き([count]
)に基づいて出力をソートしたいのですが復号化されたJSONファイルは、このように構成されています。私はすでにいくつかの方法を試してみました.がありますが、私にとってはうまくいく方法でソリューションを適用できないようです。今、私は、ソートのためにこれを探しています:
function custom_sort($a, $b) {
return $a['count'] - $b['count'];
}
usort($json, 'custom_sort');
これは(usortをスロー)、パラメータ1が配列であることを期待し、ヌル与えられ、また、2 count
人の子供(コメントや同類)がありますとにかくので、おそらくそのためにはうまくいかないでしょう。
これらのタイプの配列を扱うのはかなり新しいので、どんな助けでも大歓迎です。
だから何が不明ですか? '$ data_array'は配列ではありません。 –
@u_mulder申し訳ありませんが、私の部分にコピー&ペーストのエラーがあります。 – Lenny