3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $array = [ 'de' => [ 'label' => 'German', 'id' => 912 ], 'cn' => [ 'label' => 'Chinese', 'id' => 913 ] ]; // Initial attempt $translations = array_map( function ($v) { return $v['label']; }, $input ); var_dump($translations); // Actual //[ // 0 => [ // 'de' => 'German' // ], // 1 => [ // 'cn' 'Chinese' // ] //] // Desired //[ // 'de' => 'German', // 'cn' => 'Chinese' //] // Works declaratively via the following... $newArray = []; foreach ($array as $key => $value) { $newArray[$key] = $value['label']; } var_dump($newArray);
based on LHrtN
Output for 7.0.20, 7.1.5 - 7.3.0beta1
Notice: Undefined variable: input in /in/5WLj8 on line 19 Warning: array_map(): Argument #2 should be an array in /in/5WLj8 on line 19 NULL array(2) { ["de"]=> string(6) "German" ["cn"]=> string(7) "Chinese" }
Output for 7.0.0 - 7.0.6, 7.1.0
Notice: Undefined variable: input in /in/5WLj8 on line 18 Warning: array_map(): Argument #2 should be an array in /in/5WLj8 on line 18 NULL array(2) { ["de"]=> string(6) "German" ["cn"]=> string(7) "Chinese" }
Output for 5.5.0 - 5.6.28
Notice: Undefined variable: input in /in/5WLj8 on line 20 Warning: array_map(): Argument #2 should be an array in /in/5WLj8 on line 20 NULL array(2) { ["de"]=> string(6) "German" ["cn"]=> string(7) "Chinese" }