3v4l.org

run code in 300+ PHP versions simultaneously
<?php $queryBalance=[ (object)["balance"=>"-257,21","id_month"=>12,"year"=>"2017"], (object)["balance"=>"-257,21","id_month"=>1,"year"=>"2018"], (object)["balance"=>"-257,21","id_month"=>2,"year"=>"2018"], (object)["balance"=>"166,66","id_month"=>3,"year"=>"2018"], (object)["balance"=>"55","id_month"=>12,"year"=>"2017"], //(object)["balance"=>"0","id_month"=>1,"year"=>"2018"], (object)["balance"=>"0","id_month"=>2,"year"=>"2018"], (object)["balance"=>"55","id_month"=>3,"year"=>"2018"], (object)["balance"=>"-22257,21","id_month"=>12,"year"=>"2017"], (object)["balance"=>"-64609,14","id_month"=>1,"year"=>"2018"], (object)["balance"=>"-64609,14","id_month"=>2,"year"=>"2018"], (object)["balance"=>"-12234,89","id_month"=>3,"year"=>"2018"] ]; foreach($queryBalance as $row){ $uniques[$row->year.'-'.$row->id_month]=NULL; // determine all unique date groups } while($queryBalance){ $temp=[]; foreach($uniques as $date=>$null){ $first=current($queryBalance); // cache the element that is currently first in $queryBalance $temp[]=($date==$first->year.'-'.$first->id_month) ? array_shift($queryBalance)->balance : NULL; // extract from array, or store NULL without modifying $queryBalance } $result[]=$temp; } var_export($result);
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 ( 0 => array ( 0 => '-257,21', 1 => '-257,21', 2 => '-257,21', 3 => '166,66', ), 1 => array ( 0 => '55', 1 => NULL, 2 => '0', 3 => '55', ), 2 => array ( 0 => '-22257,21', 1 => '-64609,14', 2 => '-64609,14', 3 => '-12234,89', ), )
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 ( 0 => array ( 0 => '-257,21', 1 => '-257,21', 2 => '-257,21', 3 => '166,66', ), 1 => array ( 0 => '55', 1 => NULL, 2 => '0', 3 => '55', ), 2 => array ( 0 => '-22257,21', 1 => '-64609,14', 2 => '-64609,14', 3 => '-12234,89', ), )

preferences:
140.9 ms | 402 KiB | 157 Q