3v4l.org

run code in 300+ PHP versions simultaneously
<?php function array_remove_null($input) { foreach ($input as &$value) { if (is_array($value)) { $value = array_remove_null($value); } } return array_filter($input, function($item){ return $item !== null && $item !== ''; }); } /** * Using https://www.php.net/manual/en/function.array-replace-recursive.php */ $array1 = [ "2020-12" => [ "Mois" => "2020-12", "Nbr vendus" => null, "CA TTC" => null, "number" => "1", ], "2021-01" => [ "Mois" => "2021-01", "Nbr vendus" => null, "CA TTC" => null, "number" => "2", ], ]; $array2 = [ "2020-12" => [ "Mois" => "2020-12", "Nbr vendus" => "1", "CA TTC" => "790", "number" => null, ], "2021-01" => [ "Mois" => "2021-01", "Nbr vendus" => "3", "CA TTC" => "1680", "number" => null, ], ]; $array = array_replace_recursive(array_remove_null($array1), array_remove_null($array2)); var_dump($array);
Output for 7.3.0 - 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(2) { ["2020-12"]=> array(4) { ["Mois"]=> string(7) "2020-12" ["number"]=> string(1) "1" ["Nbr vendus"]=> string(1) "1" ["CA TTC"]=> string(3) "790" } ["2021-01"]=> array(4) { ["Mois"]=> string(7) "2021-01" ["number"]=> string(1) "2" ["Nbr vendus"]=> string(1) "3" ["CA TTC"]=> string(4) "1680" } }
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(2) { ["2020-12"]=> array(4) { ["Mois"]=> string(7) "2020-12" ["number"]=> string(1) "1" ["Nbr vendus"]=> string(1) "1" ["CA TTC"]=> string(3) "790" } ["2021-01"]=> array(4) { ["Mois"]=> string(7) "2021-01" ["number"]=> string(1) "2" ["Nbr vendus"]=> string(1) "3" ["CA TTC"]=> string(4) "1680" } }

preferences:
174.99 ms | 402 KiB | 150 Q