PHP sort JSON from highest to lowest -


i have following json:

     {     "id":"4",     "name":"gil",     "likes":0,     "dec":"a man"     },     {    "id":"3",     "name":"yoni",     "likes":3,     "dec":"a child"     },     {     "id":"6",     "name":"roni",     "likes":1,     "dec":"a woman"     } 

and rearange based on likes heighst lowest result :

{         "id":"5",         "name":"roni",         "likes":6,         "dec":"a woman"         } ,      {        "id":"3",         "name":"yoni",         "likes":3,         "dec":"a child"         },   {         "id":"4",         "name":"gil",         "likes":0,         "dec":"a man"         } 

how can rearange in php ?

$json = '[{   "id":"4",   "name":"gil",   "likes":0,   "dec":"a man" }, {   "id":"3",   "name":"yoni",   "likes":3,   "dec":"a child" }, {   "id":"6",   "name":"roni",   "likes":1,   "dec":"a woman" }]';  $json_arr = json_decode($json, true); usort(   $json_arr,    function($a, $b) {      if ($a['likes'] == $b['likes']) {       return 0;     }     return ($a['likes'] > $b['likes']) ? -1 : 1; }); $json = json_encode($json_arr); 

Comments

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -