3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php //Enter your code here, enjoy! $tests = [ [ ['X'], ['X'] ], [ ['X'], ['X'], ['X'] ], [ [ 'X', '' ], [ '', 'X' ] ], [ ['X', 'Y', 'Z'], ['X', 'Y'], ['X']], [ ['X', 'Y'], ['X', 'Y'], ['X', 'Y'] ], [ ['X', 'Y', 'Z'], ['X', 'Y', 'Z'], ['X', 'Y', 'Z'] ], [ ['X', 'Y', 'Z', 'I', 'J'], ['X', 'Y', 'Z', 'I'], ['X', 'Y', 'Z', 'I'], ['X', 'Y', 'Z', 'I'], ['X', 'Y', 'Z'], ['X', 'Y', 'Z'] ], ]; array_map(function ($matrix) { solve($matrix); }, $tests); function solve($matrix){ $master = []; $_matrix = []; foreach($matrix as $key => $array){ $_matrix[$key] = array_combine($array,$array); $master += $_matrix[$key]; } $default = array_fill_keys($master, 'null'); $result = []; foreach($_matrix as $array){ $result[] = array_values(array_merge($default, $array)); } print_r($result); }
based on mLIlQ
Output for 5.6.0 - 5.6.30, hhvm-3.18.5 - 3.22.0, 7.0.0 - 7.3.0rc2
Array ( [0] => Array ( [0] => X ) [1] => Array ( [0] => X ) ) Array ( [0] => Array ( [0] => X ) [1] => Array ( [0] => X ) [2] => Array ( [0] => X ) ) Array ( [0] => Array ( [0] => X [1] => ) [1] => Array ( [0] => X [1] => ) ) Array ( [0] => Array ( [0] => X [1] => Y [2] => Z ) [1] => Array ( [0] => X [1] => Y [2] => null ) [2] => Array ( [0] => X [1] => null [2] => null ) ) Array ( [0] => Array ( [0] => X [1] => Y ) [1] => Array ( [0] => X [1] => Y ) [2] => Array ( [0] => X [1] => Y ) ) Array ( [0] => Array ( [0] => X [1] => Y [2] => Z ) [1] => Array ( [0] => X [1] => Y [2] => Z ) [2] => Array ( [0] => X [1] => Y [2] => Z ) ) Array ( [0] => Array ( [0] => X [1] => Y [2] => Z [3] => I [4] => J ) [1] => Array ( [0] => X [1] => Y [2] => Z [3] => I [4] => null ) [2] => Array ( [0] => X [1] => Y [2] => Z [3] => I [4] => null ) [3] => Array ( [0] => X [1] => Y [2] => Z [3] => I [4] => null ) [4] => Array ( [0] => X [1] => Y [2] => Z [3] => null [4] => null ) [5] => Array ( [0] => X [1] => Y [2] => Z [3] => null [4] => null ) )