I Have a nested array with the structure:
[ 1 => [
id => id1,
label => value1
]
2 => [
id => id2,
label => value2
]
]
And I'm trying to get an array with the structure
[
id1 => value1,
id2 => value2
]
Currently, this loop works just fine.
$length = count($array);
$sortedArray = [];
for ($i = 0; $i < $length ; $i++) {
$id = $array[$i]['id'];
$value = $array[$i]['label'];
$sortedArray[$id] = $value;
}
But I'm trying to refactor it using a PHP array mapping or filtering function. I'm shooting for something like the function below, but it isn't working because I don't think I can pass $sortedArray into the mapping function.
$sortedArray = [];
array_map(function($index) {
$sortedArray[$index['id']] = $index['label'];
}, $array);
How can my loop be refactored?