0

I have the following JSON array in my PHP

 {
    "responseHeader": {
        "type": "Places",
        "status": "200",
        "message": "Places fetched"
    },
    "Places_nearby": [{
            "place_name": "blueblue",
            "place_rating": "5"


        },
        {
            "place_name": "qwer",
            "place_rating": "10"


        },
        {
            "place_name": "mvb",
            "place_rating": "0.6"


        },
        {
            "place_name": "tyu",
            "place_rating": "25"


        },
        {
            "place_name": "erty",
            "place_rating": "1"


        },

        {
            "place_name": "Malabar Adukkala",
            "place_rating": "7"

        }
    ],
    "Google_places_most_rated": [{

            "place_name": "Malabar Adukkala",
            "place_rating": "5"


        },


        {
            "place_name": "Malabar Adukkala",
            "place_rating": "5"


        }

    ]


} 

I need to sort the "Places_nearby" array inside it by increasing value of "place_rating".

What I did was decoding the JSON in my code,

json_decode($json,true)

And then using usort as follows

 function sortfunction($a, $b) {
return strcasecmp($a['place_rating'], $b['place_rating']);

}

usort($data['Places_nearby'], 'sortfunction');

But nothing happens.

How can I rewrite usort function, or do I want to write my custom function to sort the array? I thought that if PHP had some internal sporting mechanisms, that would be faster than other methods I can write.

P.S : I have already tried solutions that are mentioned in other SO answers, but they seems to be not working with my case.

5
  • 1
    Possible duplicate of sorting a multi-dimensional array Commented May 26, 2017 at 4:17
  • 1
    ...and a million others on SO... like stackoverflow.com/questions/2699086/… Commented May 26, 2017 at 4:18
  • Sorry, but those millions of solutions are not working for my case. I already gave out the code that I tested. Sorry for the possible duplicate though. Commented May 26, 2017 at 4:35
  • Here is another that does work for your case and many, many others. stackoverflow.com/a/17364128/2943403 Commented May 26, 2017 at 6:14
  • If you have control over the structure of your input array and can put place_rating before place_name in each subarray, then you can simply use: sort($array["Places_nearby"]); because it will sort on the first element in the subarrays. Demo Commented May 26, 2017 at 6:22

1 Answer 1

1

Here we are using usort for sorting.

Try this code snippet here

usort($array["Places_nearby"],function($value1,$value2){
  return  $value1["place_rating"]>$value2["place_rating"];
});
print_r($array);
Sign up to request clarification or add additional context in comments.

1 Comment

@AshifShereef welome friend... :)

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.