With PHP, I have list of Object. Each object have id field :
$myArray = [
0 => Object{
'id' => 1,
'title' => 'My title 1'
},
1 => Object{
'id' => 2,
'title' => 'My title 2'
},
2 => Object{
'id' => 6,
'title' => 'My title 6'
}
]
I want to set array key from id object without additional foreach.
I want this result :
$myArray = [
1 => Object{
'id' => 1,
'title' => 'My title 1'
},
2 => Object{
'id' => 2,
'title' => 'My title 2'
},
6 => Object{
'id' => 6,
'title' => 'My title 6'
}
]
I think it's possible with array_map but I don't know how to do it. I tried this but it returns sub array :
$newArray = array_map(function($entry) {
return [$entry->id => $entry];
}, $myArray);
// return :
[
0 => [
1 => Object{
'id' => 1,
'title' => 'My title 1'
},
],
1 => [
2 => Object{
'id' => 2,
'title' => 'My title 2'
},
],
2 => [
6 => Object{
'id' => 6,
'title' => 'My title 6'
}
]
]