1

i have the following array:

Array $a
(
[1] => Array
    (
        [id] => 1
        [name] => Shoes
    )

[2] => Array
    (
        [id] => 2
        [name] => Shirts
    )

[3] => Array
    (
        [id] => 3
        [name] => Jeans
    )
)

And i've got this array:

Array $b
(
[0] => [1]
[1] => [5]
[2] => [6]
)

Now i want to find out, if one value of $b matches the [id] in $a. If so i want to add a new value ['hasID'] => true inside $a.

The solutions should look like this:

Array $a
(
[1] => Array
    (
        [id] => 1
        [name] => Shoes
        [hasID] => true
    )

[2] => Array
    (
        [id] => 2
        [name] => Shirts
        [hasID] => false
    )

[3] => Array
    (
        [id] => 3
        [name] => Jeans
        [hasID] => false
    )
)

My try was this:

    foreach($a as $ak => $av) {
        foreach($b as $bk => $bv) {     
            if($bv == $ak['id']) {
                $a[$ak]['hasID'] == true;   
            } else {
                $a[$ak]['hasID'] == false;
            }
        }
    }

That doesn't work so. I've tried hours. Any ideas?

Thank you very much!

Regards

3 Answers 3

2

Loop through the first array and check if the value of $arr['id'] exists under the second array of ids. If found, add a flag as true or 1 , else false or 0.

foreach($arr1 as $k=>&$arr) //<--- As you can see I have  added & (Reason below)
{
   in_array($arr['id'],$arr2) ? $arr['hasID']= true : $arr['hasID']=false;
}

print_r($arr1);

Working Demo

Reason : To modify the array directly , you use the reference operator.

One-liner array_walk()

array_walk($arr1,function (&$v,$k) use($arr2){ in_array($v['id'],$arr2)? $v['hasID']=true : $v['hasID']=false; });
Sign up to request clarification or add additional context in comments.

4 Comments

You've implemented a few things there that a newcomer might not understand; references & why == won't work over =.
@MackieeE, I hope I explained the reference thing. Thanks for the headsup.
Wow! This works and is short and sexy. And i've learnt something new with the reference operator. Thank you very much! :)
@PascalCloverfield, Reduced the code even more down and added an array_walk version. Happy coding :)
0

Your try is not working because you use double == instead of simple = for value attribution. This should works :

foreach($a as $ak => $av) {
    foreach($b as $bk => $bv) {     
        if($bv == $ak['id']) {
            $a[$ak]['hasID'] = true;   
        } else {
            $a[$ak]['hasID'] = false;
        }
    }
}

However the solution with in_array is a better choice for performance reason.

Comments

0
<?php 
$arr1 = array('1'=>'shoes','2'=>'shirts','3'=>'jeans');
$arr2 = array('0'=>'1','1'=>'5','2'=>'6');

$i = 0;
foreach($arr1 as $k=>$val)
{
    if($arr2[$i] == 1)
    {
        $ARR[$k][] = $k;
        $ARR[$k][] = $val;
        $ARR[$k][] = 'true';
    }
    else
    {
        $ARR[$k][] = $k;
        $ARR[$k][] = $val;
        $ARR[$k][] = 'false';
    }
    $i++;
}
echo "<pre>";
    print_r($ARR);
echo "</pre>";
?>

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.