3v4l.org

run code in 300+ PHP versions simultaneously
<?php $product_breakdown = array( 'storage'=>array( '10001'=>array( 'total_stock'=>29, 'breakdown'=>array( '4'=>5, '1'=>5, '2'=>1, '10'=>8, '9'=>10 ) ) ) ); $orderqt = 25; if($orderqt <= $product_breakdown['storage']['10001']['total_stock']){ foreach($product_breakdown['storage']['10001']['breakdown'] as $key => &$breakdown){ if($orderqt>0){ $possibleWarehouses = array_filter( $product_breakdown['storage']['10001']['breakdown'], function ($value) use($orderqt) { return ($value >= $orderqt); } ); if(count($possibleWarehouses) != 0){ $house = key($possibleWarehouses); $order[$house] = $orderqt; $product_breakdown['storage']['10001']['breakdown'][$house] -= $orderqt; $orderqt = 0; }elseif($orderqt >= $breakdown){ $orderqt -= $breakdown; $order[$key] = $breakdown; $breakdown = 0; }else{ $order[$key] = $orderqt; $orderqt = 0; $breakdown -= $orderqt; } }else{ break; } } }else{ echo "not enough"; } var_dump($order,$product_breakdown);
Output for 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
array(5) { [4]=> int(5) [1]=> int(5) [2]=> int(1) [10]=> int(8) [9]=> int(6) } array(1) { ["storage"]=> array(1) { [10001]=> array(2) { ["total_stock"]=> int(29) ["breakdown"]=> array(5) { [4]=> int(0) [1]=> int(0) [2]=> int(0) [10]=> int(0) [9]=> &int(4) } } } }
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 array(5) { [4]=> int(5) [1]=> int(5) [2]=> int(1) [10]=> int(8) [9]=> int(6) } array(1) { ["storage"]=> array(1) { [10001]=> array(2) { ["total_stock"]=> int(29) ["breakdown"]=> array(5) { [4]=> int(0) [1]=> int(0) [2]=> int(0) [10]=> int(0) [9]=> &int(4) } } } }

preferences:
143.29 ms | 403 KiB | 156 Q