I have a dimensional array , and I would like to add an element in my lessons array
this is my array
$array = [
[
'id' => 1,
'lessons' => [
[
'name' => 'PHP',
],
[
'name' => 'Python',
]
]
],
[
'id' => 1,
'lessons' => [
[
'name' => 'Java',
],
[
'name' => 'Ruby',
]
]
],
];
I would like to add flag element to my lessons ,
Desired output:
$array = [
'id' => 1,
'lessons' => [
[
'name' => 'PHP',
'flag' => true
],
[
'name' => 'Python',
'flag' => true
]
]
];
I would like to do that without a nested foreach or a foreach . I tried with array_map
Code I tried:
$csmap_data = array_map(function($array){
return $topics['lessons'] + ['flag' => true];
}, $topics);
Try this:
$array['lessons'] = array_map(function($lesson) {
$lesson['flag'] = true;
return $lesson;
}, $array['lessons']);
print_r($array);
foreach( $array['lessons'] as $lesson )
{
$lesson['flag'] = true;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.