来自json结果的我的数组返回如下:
["AuctionInfo"]=>
[0]=>
array(13) {
["price"]=>
int(3000)
}
[1]=>
array(13) {
["price"]=>
int(5000)
我试图使用的PHP代码很简单
$decoded = json_decode("my json api", true);
foreach($decoded['AuctionInfo'] as $item) {
usort($decoded['AuctionInfo']['price']);?>
<?php echo $item['price']; ?>我尝试在我的foreach之后添加usort($decoding [‘AuctionInfo’] [‘price’]但是这只会引发错误,是否有人能指出我正确的方向?
我想做的就是按降序排序$decoding [‘AuctionInfo’] [‘price’]
解决方法:
这样的事情应该有效.
$data = array(
'AuctionInfo' => array(
array('price' => 500),
array('price' => 3000)
)
);
$decoded = $data['AuctionInfo'];
foreach($decoded as $item) {
echo $item['price'] . PHP_EOL;
}
usort($decoded, function($a, $b) { return $a['price'] < $b['price'] ? 1 : -1; });
foreach($decoded as $item) {
echo $item['price'] . PHP_EOL;
}
标签:php,json
来源: https://codeday.me/bug/20190729/1568374.html