- json_decode: documentation ( source)
- print_r: documentation ( source)
- usort: documentation ( source)
<?php
class Products
{
public static function sortByPriceAscending($jsonString)
{
$info = json_decode($jsonString, true);
print_r($info);
usort($info, function($a, $b)
{
return $a['price'] - $b['price'];
});
print_r($info);
}
}
// For testing purposes (do not submit uncommented)
echo Products::sortByPriceAscending('[{"name":"coffee","price":9.99},{"name":"eggs","price":1},{"name":"rice","price":4.04}]');