2

I have two arrays and I need remove from bigger array smaller

$a = array(223 => 6, 381 => 6);

and second arrays is:

$b = array(array('id' => 45, 'username' => 'rock'), array('id' => 223, 'username' => 'pop'), array('id' => 381, 'username' => 'stock'));

With print_r() they looks like this

Array
(
    [223] => 6
    [381] => 6
)

Array
(
    [0] => Array
        (
            [id] => 45
            [username] => rock
        )

    [1] => Array
        (
            [id] => 223
            [username] => pop
        )

    [2] => Array
        (
            [id] => 381
            [username] => stock
        )

)

How do I remove from $b array where id != $a[keys] So in result I will have array like this

Array
(
    [0] => Array
        (
            [id] => 223
            [username] => pop
        )

    [1] => Array
        (
            [id] => 381
            [username] => stock
        )

)

I try to use array_diff, array_search and unset but do not know how make it work.

4 Answers 4

6

So, you want to filter out the values where no $a[$sub_array["id"]] exists, where $sub_array is an element of $b:

Then array_filter() is what you search:

$array = array_filter($b, function ($val) use ($a) {
    return isset($a[$val["id"]]);
});
Sign up to request clarification or add additional context in comments.

2 Comments

your varian is more cleaner but it shows this error array_filter() expects parameter 1 to be array
@user3375344 true, that's because I didn't respect parameter order; fixed.
1
    $a = array(223 => 6, 381 => 6);
    $b = array(array('id' => 45, 'username' => 'rock'), array('id' => 223, 'username' => 'pop'), array('id' => 381, 'username' => 'stock'));
    $keys = array_keys($a);
    $new_array = array();
    for ($i = 0, $len = count($b); $i < $len; $i++) {
        $c = $b[$i];
        if (in_array($c['id'], $keys)) {
            $new_array[] = $c;
        }
    }
    var_dump($new_array);

1 Comment

Check out bwoebi's answer for a cleaner approach.
0
<?php
$a = array(223 => 6, 381 => 6);
$b = array(array('id' => 45, 'username' => 'rock'), array('id' => 223, 'username' => 'pop'), array('id' => 381, 'username' => 'stock'));
$cc = 0;
foreach($b as $bb)
{
    $flag = 0;
    foreach($a as $k => $aa)
    {
        if($bb['id'] == $k)
        {
            $flag=1;
        }
    }
    if($flag == 0)
    {
        unset($b[$cc]);
    }
    $cc++;
}
print_r($b);

Comments

0

You want first to use array_keys to get all the keys from $a. Then loop through $b and with in_array you will know if id is a $a key. Finally use unset.

$a = array(223 => 6, 381 => 6);
$aKey = array_keys($a);
$b = array(array('id' => 45, 'username' => 'rock'), array('id' => 223, 'username' => 'pop'), array('id' => 381, 'username' => 'stock'));
foreach ($b as $key => $value)
    if (!in_array($value['id'], $aKey))
        unset($b[$key]);
print_r($b);

1 Comment

array_keys obviates the need for your first loop.

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.