~~~
function dump($data) {
echo "<pre>";
print_r($data);
echo "</pre>";
die();
}
$arr = [
[
'price' => 10.5,
'sales' => 10,
'sales2' => 100
],
[
'price' => 11.5,
'sales' => 20,
'sales2' => 200
],
[
'price' => 30.5,
'sales' => 30,
'sales2' => 300
],
[
'price' => 40.5,
'sales' => 40,
'sales2' => 400
],
[
'price' => 50.5,
'sales' => 50,
'sales2' => 500
],
];
$price_column = array_column($arr, 'price');
$min_price = min($price_column);
$max_price = max($price_column);
$diff_price = bcsub($max_price, $min_price, 2);
$diff = $diff_price / 10;
$new_arr = [];
$new_arr2 = [];
for($i = 0; $i < 10; $i++) {
$price1 = $min_price + $i * $diff;
$price2 = $min_price + ($i+1) * $diff;
$y = $y2 = 0;
foreach ($arr as $key => $value) {
if(bccomp($price1, $value['price']) != 1 && bccomp($price2, $value['price']) != -1) {
$y += $value['sales'];
$y2 += $value['sales2'];
}
}
$new_arr[$i]['name'] = $price1 . '-' . $price2;
$new_arr[$i]['y'] = $y;
$new_arr2[$i]['name'] = $price1 . '-' . $price2;
$new_arr2[$i]['y'] = $y2;
}
dump($new_arr2);
exit;
~~~