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;
}
3
  • 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. Commented Nov 25, 2016 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 stackoverflow.com/a/8102246/689579 Commented Nov 25, 2016 at 22:13
  • @Sean I posted an answer below before realizing someone else answered. Both are different Commented Nov 25, 2016 at 22:23

3 Answers 3

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
        )
)
Sign up to request clarification or add additional context in comments.

2 Comments

The only issue here is it requires all my key/values on the $list_tags array to be an exact match and not just the id keys
it will search for an occurrence of the whole nested array, not just id key
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';
    }
}

Comments

-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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.