0

i want to compare 2 arrays. If a value from $a ist found is $b there should be make a new value in $a like [found] => true.

Array $a:

Array
(
[1] => Array
    (
        [grpID] => 1
        [groupname] => Marketing
    )

[2] => Array
    (
        [grpID] => 2
        [groupname] => Vertrieb
    )

[4] => Array
    (
        [grpID] => 4
        [groupname] => Produktion
    )

)

Array $b:

Array
(
[1] => Array
    (
        [usrID] => 23
        [grpID] => 1
    )

)

So now i want to compare these two.

The result should look like the following, because $b[1]['grpID'] was like in $a[1]['grpID']:

Array $c (or $a as manipulated?):

Array
(
    [1] => Array
    (
        [grpID] => 1
        [groupname] => Marketing
        [found] => true
    )
    [2] => Array
    (
        [grpID] => 2
        [groupname] => Vertrieb
        [found] => false
    )
)

The size of $b may vary so i don't think i can work with a for-loop, can i?

Sorry, i've got no code so far as i don't have an idea how to start.

Thanks in advance.

Best regards

3
  • You can do it using nested loops Commented Apr 9, 2014 at 13:48
  • You want to compare only grpID or grpName also? Commented Apr 9, 2014 at 13:51
  • If $b[$i]['grpID'] is in $a[$i]['grpID'] make new value in $a[$i] with [found] => true. Commented Apr 9, 2014 at 13:55

3 Answers 3

1

Do it with an temporary array $temp.

$a = array();
$b = array();
$c = array();
$tmp = array();

$a[1] = array('grpID' => 1, 'groupname' => 'Marketing');
$a[2] = array('grpID' => 2, 'groupname' => 'Vertrieb');
$a[4] = array('grpID' => 4, 'groupname' => 'Produktion');

$b[1] = array('usrID' => 23, 'grpID' => 1);

foreach ($b as $key => $value) {
    $tmp[] = $value['grpID'];
}

foreach ($a as $key => $value) {
        $c[] = array_merge($value, array('found' => (in_array($value['grpID'], $tmp) ? 'true' : false)));
}
Sign up to request clarification or add additional context in comments.

1 Comment

Perfekt. Since i've got alot of this code alreay this is a short solution. You're the best man! Thanks alot! :)
1

Checkout the following code

<?php
$a = array(
    array(
        'grpID' => 1,
        'groupname' => 'Marketing'
    ),
    array(
        'grpID' => 2,
        'groupname' => 'Vertrieb'
    ),
    array(
        'grpID' => 5,
        'groupname' => 'Produktion'
    )
);
$b = array(
    array(
        'grpID' => 1,
        'usrID' => 23
    )
);
$c = array();

for ($i = 0; $i < count($a); $i++)
{
    $tmp = $a[$i];
    $tmp['found'] = FALSE;
    for ($j = 0; $j < count($b); $j++)
    {
        if ($tmp['grpID'] === $b[$j]['grpID'])
        {
            $tmp['found'] = TRUE;
            break;
        }
    }
    array_push($c, $tmp);
}

// Final output
print_r($c);
?>

Comments

0

There are a few different ways to do this, I toyed with the idea of using array_walk and using a object orientated approach. The way below is the simplest to implement but if I were doing this on a system of my own I would probably go object orientated for cleaner code and better maintenance.

here are the help pages for array_walk https://www.php.net/array_walk just in case you wanted to look at another way.

$foundGroups = array();

//set all found values to false
foreach ($a as $group) {
   $group['found'] = false;
}

foreach ($b as $user) {
    $groupToFind = $user['grpID'];
    
    // This is to limit the number of times we do this, there is no point
    //  in looking for the same group twice
    if (!in_array($groupToFind, $foundGroups)) {
        foreach ($a as $group) {
           if ($group['grpID'] == $groupToFind) {
                $group['found'] = 'true';
                $foundGroups[] = $groupToFind;
           }
        }
    }
}

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.