2

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;
}
JasonDavis
  • 48,204
  • 100
  • 318
  • 537
  • Are `$all_tags` and `$list_tags` ordered? 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_ids` from `$list_tags` first, then check each item of `$all_tags` against them. – raina77ow Nov 25 '16 at 22:13
  • What have you tried? You will need to search your `$list_tags` array, and since it is a multidimensional array also, it will require a loop. something like http://stackoverflow.com/a/8102246/689579 – Sean Nov 25 '16 at 22:13
  • @Sean I posted an answer below before realizing someone else answered. Both are different – JasonDavis Nov 25 '16 at 22:23

3 Answers3

2

In your simple case it would be enough to use a regular foreach loop with in_array function:

...
foreach ($all_tags as &$v) {
    if (in_array($v, $list_tags)) {
        $v['class'] = 'added';
    }
}

print_r($all_tags);

The output:

Array
(
    [0] => Array
        (
            [id] => 1
            [title] => title 1
            [description] => description 1
            [class] => added
        )

    [1] => Array
        (
            [id] => 2
            [title] => title 2
            [description] => description 2
            [class] => added
        )

    [2] => Array
        (
            [id] => 3
            [title] => title 3
            [description] => description 3
        )

    [3] => Array
        (
            [id] => 4
            [title] => title 4
            [description] => description 4
        )

    [4] => Array
        (
            [id] => 5
            [title] => title 5
            [description] => description 5
            [class] => added
        )
)
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
1

My suggestion is to get the $list_tags ids first and then iterate only over $all_tags .

This approach will work when the arrays are not exactly the same but the id matches.

$list_tags_ids = array_column($list_tags, 'id');

foreach($all_tags as &$val){
    if(in_array($val['id'], $list_tags_ids)) {
        $val['class'] = 'added';
    }
}
108
  • 370
  • 5
  • 18
-1

I got this method to do the desired output. I am always open to other ways too =)

$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 $allTagKey => $allTagElement) {
    foreach ($list_tags as $listTagKey => $listTagElement) {
        if ($allTagElement['id'] == $listTagElement['id']) {
            $all_tags[$allTagKey]['class'] = 'added';
            break; // for performance and no extra looping
        }
    }
}

echo '<pre>';
print_r($all_tags);
JasonDavis
  • 48,204
  • 100
  • 318
  • 537