json 배열을 받아서 value 값을 정렬하여서 화면에 보여주려고 해서 배열선언이 필요했다.
<?php
// 그룹에 대한 정보를 저장
header('Content-Type: application/json');
$groupData = array();
$groupData["groupName"] = "서태지와 아이들";
$groupData["debutYear"] = "1992";
$groupData["memberCount"] = "3";
// 첫 번째 코드와 동일
$member1 = array("name" => "서태지", "height" => "173cm", "weight" => "55kg");
$member2 = array("name" => "양현석", "height" => "180cm", "weight" => "70kg");
$member3 = array("name" => "이주노", "height" => "172cm", "weight" => "53kg");
$memberData = array($member1, $member2, $member3);
// JSON Array를 연관 배열로 저장 키이름은 memberData
$groupData["memberData"] = $memberData;
// JSON Array가 포함된 Object를 문자열로 변환
$output = json_encode($groupData);
// 출력
echo urldecode($output);
?>
$data = json_encode($data, true);
usort($data, function ($a, $b) {
return $a['post_count'] <=> $b['post_count'];
});
$array = json_decode($data, true);
array_multisort(array_column($array, 'post_count'), SORT_ASC, $array);
echo json_encode($array, JSON_PRETTY_PRINT);
참조
https://blog.devez.net/ko/257
https://stackoverflow.com/questions/55488924/how-to-sort-json-array-in-php-by-key