3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function reindexArray($array, $column) { return array_combine( array_map(function($x) use ($column) { return $x[$column]; }, $array), $array ); } function arrayJoin($array1, $array2, $column, $default=null) { //in PHP 5.5 it's just array_column() call: $array1 = reindexArray($array1, $column); $array2 = reindexArray($array2, $column); $blank1 = array_combine( array_keys(current($array1)), array_fill(1, count(current($array1)), $default) ); $blank2 = array_combine( array_keys(current($array2)), array_fill(1, count(current($array2)), $default) ); unset($blank1[$column], $blank2[$column]); return array_merge( array_map(function($x) use ($array1, $blank2) { return array_merge($array1[$x], $blank2); }, array_keys(array_diff_key($array1, $array2))), array_map(function($x) use ($array1, $array2) { return array_merge($array1[$x], $array2[$x]); }, array_keys(array_intersect_key($array1, $array2))), array_map(function($x) use ($array2, $blank1) { return array_merge($array2[$x], $blank1); }, array_keys(array_diff_key($array2, $array1))) ); } $array1 = array(); $array1[]=array('subscribers'=>2,'dt_date'=>'2014-02-27'); $array1[]=array('subscribers'=>2,'dt_date'=>'2014-02-25'); $array1[]=array('subscribers'=>1,'dt_date'=>'2014-02-07'); $array2 = array(); $array2[]=array('unsubscribers'=>1,'dt_date'=>'2014-02-27'); $array2[]=array('unsubscribers'=>1,'dt_date'=>'2014-02-01'); var_dump(arrayJoin($array1, $array2, 'dt_date', 0));
Output for 5.3.0 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
array(4) { [0]=> array(3) { ["subscribers"]=> int(2) ["dt_date"]=> string(10) "2014-02-25" ["unsubscribers"]=> int(0) } [1]=> array(3) { ["subscribers"]=> int(1) ["dt_date"]=> string(10) "2014-02-07" ["unsubscribers"]=> int(0) } [2]=> array(3) { ["subscribers"]=> int(2) ["dt_date"]=> string(10) "2014-02-27" ["unsubscribers"]=> int(1) } [3]=> array(3) { ["unsubscribers"]=> int(1) ["dt_date"]=> string(10) "2014-02-01" ["subscribers"]=> int(0) } }
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.2.17
Parse error: syntax error, unexpected T_FUNCTION, expecting ')' in /in/lUAeN on line 5
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_FUNCTION, expecting ')' in /in/lUAeN on line 5
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `')'' in /in/lUAeN on line 5
Process exited with code 255.