我正在使用 PHP 从 Web 服务返回 JSON。我能够获取 JSON,对其进行解码并查看结果。但是,我需要能够按特定值对数组进行排序。我目前有:
// JSON URL which should be requested
$json_url = 'https://*******/maincategories';
// Initializing curl
$ch = curl_init( $json_url );
// Configuring curl options
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array('Content-type: application/json') ,
);
// Setting curl options
curl_setopt_array( $ch, $options );
// Getting results
$result = curl_exec($ch); // Getting JSON result string
$data = json_decode($result);
ksort($data, "Total");
print_r($data);
print_r($data);
打印如下:
Array ( [0] => stdClass Object ( [Goal] => 10000000 [Name] => Rental [Total] => 500000 ) [1] => stdClass Object ( [Goal] => 8000000 [Name] => National Sales [Total] => 750000 ) [2] => stdClass Object ( [Goal] => 120000000 [Name] => Vendor Leasing [Total] => 500000 ) )
我试图使用 ksort 并通过键按升序对数组进行排序Total
。如何对这个数组进行排序,使总数最高的对象排在第一位,其余的按升序排列?