3v4l.org

run code in 300+ PHP versions simultaneously
<?php $a = [ 0 => [ 'period' => '2017/2018', 'product' => 'AM', 'quantity_1' => 20, 'quantity' => 25, ], 1 => [ 'period' => '2018/2019', 'product' => 'AM', 'quantity_1' => 12, 'quantity' => 19, ], 2 => [ 'period' => '2017/2018', 'product' => 'DC', 'quantity_1' => 20, 'quantity' => 25, ], 3 => [ 'period' => '2018/2019', 'product' => 'DC', 'quantity_1' => 8, 'quantity' => 10, ] ]; $temp = []; foreach ($a as $x){ $temp[$x['product']][$x['period']] = $x; } print_r($temp); $result = []; foreach ($temp as $key => $res){ $result[] = [ 'product' => $key, 'quantity_1' => $res['2018/2019']['quantity_1'] / $res['2017/2018']['quantity_1'], 'quantity_2' => $res['2018/2019']['quantity'] / $res['2017/2018']['quantity'], ]; } print_r($result);
Output for 7.1.0 - 7.1.33, 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.28, 8.4.1 - 8.4.14, 8.5.0
Array ( [AM] => Array ( [2017/2018] => Array ( [period] => 2017/2018 [product] => AM [quantity_1] => 20 [quantity] => 25 ) [2018/2019] => Array ( [period] => 2018/2019 [product] => AM [quantity_1] => 12 [quantity] => 19 ) ) [DC] => Array ( [2017/2018] => Array ( [period] => 2017/2018 [product] => DC [quantity_1] => 20 [quantity] => 25 ) [2018/2019] => Array ( [period] => 2018/2019 [product] => DC [quantity_1] => 8 [quantity] => 10 ) ) ) Array ( [0] => Array ( [product] => AM [quantity_1] => 0.6 [quantity_2] => 0.76 ) [1] => Array ( [product] => DC [quantity_1] => 0.4 [quantity_2] => 0.4 ) )
Output for 8.4.15
/bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.35' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.34' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15)
Process exited with code 1.

preferences:
348.27 ms | 408 KiB | 5 Q