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;
}
$all_tagsand$list_tagsordered? I mean, does they both always start from items with lowest 'id'? In this case, it's rather straight-forward; otherwise you'd better create a hash of$used_idsfrom$list_tagsfirst, then check each item of$all_tagsagainst them.$list_tagsarray, and since it is a multidimensional array also, it will require a loop. something like stackoverflow.com/a/8102246/689579