Pascal Cloverfield
Pascal Cloverfield

Reputation: 581

PHP Merge 2 arrays if value is found and create new value

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

Upvotes: 0

Views: 49

Answers (3)

dops
dops

Reputation: 800

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

Upvotes: 0

Jérôme
Jérôme

Reputation: 292

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

Upvotes: 1

dikesh
dikesh

Reputation: 3125

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

Upvotes: 1

Related Questions