1

I have an array like this

Array
(
    [0] => Array
        (
            [0] => 83
            [1] => 82
            [2] => 81
        )

    [1] => Array
        (
            [0] => 81
            [1] => 82
            [2] => 83
            [3] => 100
            [4] => 101
            [5] => 102
            [6] => 103
            [7] => 104
            [8] => 105
        )

)

and I want to delete any values from the first array that are not equal to this array

Array
(
    [0] => 83
    [1] => 82
    [2] => 81
)

But i want to keep the same structure as the first array. So i would end up with something like this

Array
(
    [0] => Array
        (
            [0] => 83
            [1] => 82
            [2] => 81
        )

    [1] => Array
        (
            [0] => 81
            [1] => 82
            [2] => 83
        )

)

What is the best way to achieve this?

3
  • I need more info on the "keeping the same structure" though. Do you need the same keys too ? Same order ? Commented Oct 28, 2011 at 11:06
  • @tom no the keys don't really matter or the order of the values, it just needs to be a multidimensional array. Thanks Commented Oct 28, 2011 at 11:07
  • Then my example should do it. Commented Oct 28, 2011 at 11:08

4 Answers 4

4
$data = array( /* your data you want to check (first big array) */ );
$test = array( 83, 82, 81 );

foreach ( $data as $key => $value ) {
  $intersect = array_intersect($value, $test);
  if ( $intersect != $test ) {
    unset($data[$key]);
  } else {
    $data[$key] = $intersect;
  }
}
Sign up to request clarification or add additional context in comments.

Comments

2

if $array is your starting array and $matches is the array of values you want to match;

foreach ($array as $key => $subarray) {
    foreach ($subarray as $subsubarray) {
        foreach ($matches as $match) {
            if ($subsubarray == $match) {
                $finalarr[$key][] = $subsubarray;
            }
        }
    }
}

$finalarr will be your desired result.

Comments

1

You can always set the array to be the intersection of the first and second one with the array_intersect function.

<?php
$array[1] = array_intersect($array[0], $array[1]);
?>

1 Comment

$array1 = ["2"], $array2 = ["2","2","7","7"], array_intersect($array1, $array2)=["7"] but array_intersect($array2, $array1)=["7", "7"]
0
$temp = array
(
    0 => array
    (
        0 => 83,
        1 => 82,
        2 => 81
    ),

    1 => array
    (
        0 => 81,
        1 => 82,
        2 => 83,
        3 => 100,
        4 => 101,
        5 => 102,
        6 => 103,
        7 => 104,
        8 => 105
    )

);

for($i=0,$tot = count($temp[1]);$i<$tot;$i++)
{
    if(!in_array($temp[1][$i],$temp[0]))
    {
        unset($temp[1][$i]);
    }
}

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.