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);
0

1 Answer 1

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à.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.