Below I have 2 PHP arrays. $all_tags
and $list_tags
I need to iterate over the $all_tags
array and add a key/value 'class' => 'added'
to each array item in $all_tags
that has a matching array item with the same ID in the $list_tags
array.
In this sample data below that would mean in the $all_tags
array the items with id
value of 1, 2, and 5 would have a key named class
with a value of added
.
$all_tags = array();
$all_tags[] = array(
'id' => 1,
'title' => 'title 1',
'description' => 'description 1'
);
$all_tags[] = array(
'id' => 2,
'title' => 'title 2',
'description' => 'description 2'
);
$all_tags[] = array(
'id' => 3,
'title' => 'title 3',
'description' => 'description 3'
);
$all_tags[] = array(
'id' => 4,
'title' => 'title 4',
'description' => 'description 4'
);
$all_tags[] = array(
'id' => 5,
'title' => 'title 5',
'description' => 'description 5'
);
$list_tags = array();
$list_tags[] = array(
'id' => 1,
'title' => 'title 1',
'description' => 'description 1'
);
$list_tags[] = array(
'id' => 2,
'title' => 'title 2',
'description' => 'description 2'
);
$list_tags[] = array(
'id' => 5,
'title' => 'title 5',
'description' => 'description 5'
);
foreach($all_tags as $key => $val){
echo $val;
}