2

i have the following response, how to sort it depending on the distnace

{
  "Message": "Done.",
  "Status": true,
  "InnerData": [
      {
         "id": 66,
         "name": "tito",
         "distance": 74,
      },
      {
         "id": 67,
         "name": "liver pool",
         "distance": 83
      },
      {
         "id": 67,
         "name": "Text",
         "distance": 72
      }
  ]
}

i tried the usort but i didn't make it. i also tried this answer here but it seems to be different than the one i need

Rami
  • 161
  • 3
  • 18
  • you can sort it in sql. But in your situation just use foreach loops and sort it manual. – MRustamzade May 21 '18 at 14:08
  • You need to decode the JSON into a PHP data structure, sort it, then re-encode it. Post what you've tried and we can help you debug it. – iainn May 21 '18 at 14:11
  • the distance parameter is not a sql field, i calculate it and pass it in the response. can you please give me an example how to sort it using foreach ? – Rami May 21 '18 at 14:11
  • https://stackoverflow.com/questions/1597736/how-to-sort-an-array-of-associative-arrays-by-value-of-a-given-key-in-php – MRustamzade May 21 '18 at 14:16

3 Answers3

4

In pure PHP 7

<?php

$json = '{
  "Message": "Done.",
  "Status": true,
  "InnerData": [
      {
         "id": 66,
         "name": "tito",
         "distance": 74
      },
      {
         "id": 67,
         "name": "liver pool",
         "distance": 83
      },
      {
         "id": 67,
         "name": "Text",
         "distance": 72
      }
  ]
}';
$array = json_decode($json, true);
usort($array['InnerData'], function($a, $b) {
    return $a['distance'] <=> $b['distance'];
});

print_r($array);
Pyton
  • 1,291
  • 8
  • 19
  • 1
    This is great ! Could you also perform additional checks for `id` and `name` as `usort()` doesn't guarantee a stable sort for order of equal elements. In this context, the OP might want to preserve the order. – nice_dev May 21 '18 at 14:46
3

As @hdifen suggested, if you're using Laravel it's a breeze to do this.

$json = '{
  "Message": "Done.",
  "Status": true,
  "InnerData": [
      {
         "id": 66,
         "name": "tito",
         "distance": 74
      },
      {
         "id": 67,
         "name": "liver pool",
         "distance": 83
      },
      {
         "id": 67,
         "name": "Text",
         "distance": 72
      }
  ]
}';

$data = json_decode($json, true);

$data['InnerData'] = collect($data['InnerData'])->sortBy('distance', SORT_REGULAR, true);

$encoded = json_encode($data);

echo $encoded;

Output:

{  
   "Message":"Done.",
   "Status":true,
   "InnerData":{  
      "1":{  
         "id":67,
         "name":"liver pool",
         "distance":83
      },
      "0":{  
         "id":66,
         "name":"tito",
         "distance":74
      },
      "2":{  
         "id":67,
         "name":"Text",
         "distance":72
      }
   }
}
Steve Bauman
  • 8,165
  • 7
  • 40
  • 56
0

Json is mainly used as a common format for sending data.

In Laravel you can convert a json object to a php array easily by using json_decode().

$phpArray = json_decode($json);

From here you can convert it to a collection to take advantage of laravels collection functions.

$laravelArray = collect($phpArray);

After this take a look at https://laravel.com/docs/5.5/collections to do sort/filter or do whatever you want to the array.

Tom Headifen
  • 1,885
  • 1
  • 18
  • 35