3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php function remap($e) { $elements = array( 1 => 'first_name', 2 => 'city', 3 => 'email', 4 => 'phone' ); $key = isset($elements[$e['element_id']]) ? $elements[$e['element_id']]: 'unknown'; return array($key => $e['value']); } $input[] = array('entry_id' => 33, 'element_id' => 1, 'value' => 'Fred'); $input[] = array('entry_id' => 33, 'element_id' => 2, 'value' => 'Phoenix'); $input[] = array('entry_id' => 33, 'element_id' => 3, 'value' => 'fred@test.com'); $input[] = array('entry_id' => 33, 'element_id' => 99, 'value' => 'Something else entirely'); $output = array(); foreach(array_map('remap', $input, $elements) as $key => $value) { $output[$key] = $value; }; var_dump($output);
based on A1TtT
Output for 5.6.0 - 5.6.30, 7.0.0 - 7.3.0beta1
Notice: Undefined variable: elements in /in/nNIpf on line 23 Warning: array_map(): Argument #3 should be an array in /in/nNIpf on line 23 Warning: Invalid argument supplied for foreach() in /in/nNIpf on line 23 array(0) { }
Output for hhvm-3.22.0
Notice: Undefined variable: elements in /in/nNIpf on line -1 Warning: array_map(): Argument #3 should be an array or collection in /in/nNIpf on line -1 array(4) { [0]=> array(1) { ["first_name"]=> string(4) "Fred" } [1]=> array(1) { ["city"]=> string(7) "Phoenix" } [2]=> array(1) { ["email"]=> string(13) "fred@test.com" } [3]=> array(1) { ["unknown"]=> string(23) "Something else entirely" } }
Output for hhvm-3.21.3
Notice: Undefined variable: elements in /in/nNIpf on line 23 Warning: array_map(): Argument #3 should be an array or collection in /in/nNIpf on line 25 array(4) { [0]=> array(1) { ["first_name"]=> string(4) "Fred" } [1]=> array(1) { ["city"]=> string(7) "Phoenix" } [2]=> array(1) { ["email"]=> string(13) "fred@test.com" } [3]=> array(1) { ["unknown"]=> string(23) "Something else entirely" } }
Output for hhvm-3.18.5
Notice: Undefined variable: elements in /in/nNIpf on line 23 Warning: array_map(): Argument #3 should be an array or collection array(4) { [0]=> array(1) { ["first_name"]=> string(4) "Fred" } [1]=> array(1) { ["city"]=> string(7) "Phoenix" } [2]=> array(1) { ["email"]=> string(13) "fred@test.com" } [3]=> array(1) { ["unknown"]=> string(23) "Something else entirely" } }