...
[188]=>
array(4) {
["id"]=>
string(4) "1201"
["coins_id"]=>
string(2) "22"
["price"]=>
string(8) "11.42033"
["time"]=>
string(19) "2018-04-19 09:45:56"
}
...
[534]=>
array(4) {
["id"]=>
string(4) "9391"
["coins_id"]=>
string(2) "22"
["price"]=>
string(8) "17.87132"
["time"]=>
string(19) "2018-04-19 10:09:17"
}
...
我想计算价格的百分比差异,但我不知道如何将最年轻和最旧的值分配给变量。在这个json中,旧值总是小id
。常见值为coins_id
。
$price_young = //price with id = 534;
$price_old = //price with id = 188;
$percent_diffrent = (1 - $price_old / $price_young) * 100;
答案 0 :(得分:1)
您可以使用max()和min()以及array_column()
$price_array = array_column($array,'price'); // get all prices in an array
$price_young = (float)max($price_array); // get max latest price
$price_old = (float)min($price_array); // get min old price
$percent_diffrent = (1 - $price_old / $price_young) * 100;
一个硬编码示例: - https://eval.in/990963