3v4l.org

run code in 300+ PHP versions simultaneously
<?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); }

preferences:
54.48 ms | 402 KiB | 5 Q