I have to sort names according to serial nos. Following is the code which I want to sort by the serial no.
$data = json_decode($result,true);
$v1 = $data['Data'];
foreach($v1 as $key => $value)
{
$name = $value['Name'];
$order = $value['SerialNo'];
echo $name . '<br>';
echo $order . '<br>';
}
I have tried sort($v1);
which sort the whole data but not serial wise. I have tried the following;
function srt($a, $b)
{
if ($a < $b) {
return -1;
} else if ($a > $b) {
return 1;
} else {
return 0;
}
}
usort($v1, 'srt');
JSON data
{"Math":{"Name":"John","SerialNo":"4"},"Science":{"Name":"Peter","SerialNo":"1"},"English":{"Name":"Kyle","SerialNo":"3"},"German":{"Name":"Neena","SerialNo":"2"},"Art":{"Name":"Rita","SerialNo":"5"}}
This doesn't sort the data as per serial no. If I put in $order instead of $v1 in function I get error that function requires array and string is given instead.
How to sort the names according to serial no. then?
thanks, AK