- array_map: documentation ( source)
- var_dump: documentation ( source)
<?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);