I want to sort below multilevel array on ascending order of price value.
{
"pData": [
{
"name": "Walmart",
"address": "Full Address for Walmart",
"pricing": [{
"id": "Samsung Galaxy 21M",
"price": 157.0,
}],
},
{
"name": "Stop & Shop",
"address": "Full Address for Stop & Shop",
"pricing": [{
"id": "Samsung Galaxy 21M",
"price": 142.0,
}],
},
{
"name": "Safeway",
"address": "Full Address for Safeway",
"pricing": [{
"id": "Samsung Galaxy 21M",
"price": 148.0,
}],
}
],
"status ":" ok "
}
I tried below code but giving same result without sorting array on value of price key. I even tried many similar codes from different answers on this site but none of working. So please advise what is wrong with my code or suggest completely different code.
function cmp($a, $b)
{
//if ($a["price"] == $b["price"]) // Try #1
if($a["pData"]["pricing"]["price"] == $b["pData"]["pricing"]["price"]) // Try #2
{
return 0;
}
return ($a["price"] < $b["price"]) ? -1 : 1; // Try #1
return ($a["pData"]["pricing"]["price"] < $b["pData"]["pricing"]["price"]) ? -1 : 1;
}
usort($result,"cmp");
print_r($result);