0

I have a json file looks like this..

{
   "movies":[
      {
         "title":"Movie 1",
         "year":"2014",
         "categories":"Action,Sci-Fi",
         "rate":"10",
         "tags":"720p,1080p,Direct",
         "image_link":"img/1.jpg",
         "imdb_link":"http://www.imdb.com"
      },
      .
      .
      .

I need to sort this data using some options like by year, rate, alphabetically ascending and descending.

Here is the code of fetching the data

$jsondata = file_get_contents('data/movies.json');
$movies = json_decode($jsondata, true);
Momen Sh
  • 3
  • 2

1 Answers1

3

If i may draw from this answer

function sortByOrder($a, $b) {
    return $a['order'] - $b['order'];
}

usort($myArray, 'sortByOrder');

Which, in your case, just needs to be modified to have different functions that sort by different things, like this:

function sortByRate($a, $b) {
    return $a['rate'] - $b['rate'];
}

And etcetera, you get the gist.

Afterwards, you just have to call usort with the appropriate sorting function as parameter, and voilĂ .

Community
  • 1
  • 1
Adrian Todorov
  • 289
  • 1
  • 2
  • 11