Check if array elements exist in another nested array, and remove those that do not

78 Views Asked by At

I have an array with some values ​​and I need to check if some values ​​are not in this array.

This is the first array with name "$arr_cls".

Array ( 
  [0] => Array ( 
    [0] => Paris SG 
    [1] => Brest 
    [2] => Monaco 
    [3] => Nizza 
    [4] => Lilla 
    [5] => Lens )

  [1] => Array ( 
    [0] => Marsiglia 
    [1] => Rennes 
    [2] => Reims 
    [3] => Lione 
    [4] => Tolosa 
    [5] => Strasburgo ) 

  [2] => Array ( 
    [0] => Le Havre 
    [1] => Montpellier 
    [2] => Lorient 
    [3] => Nantes 
    [4] => Metz 
    [5] => Clermont ) 
)

the second array: with name "$part_t2"

Array ( 
  [0] => Sturm Graz 
  [1] => Rennes 
  [2] => Sturm Graz 
  [3] => Reims 
  [4] => Tolosa 
  [5] => Le Havre 
  [6] => Paris SG 
  [7] => Lione 
  [8] => Clermont 
  [9] => Montpellier 
  [10] => Lorient 
  [11] => Strasburgo )

i need to remove from second array the value that not exist into first array in this case value[0] and value[2] (Sturm Graz).

i try this but not work.

$part_t2_2=array();
foreach($part_t2 as $value){
    
    if (in_array($value,$arr_cls[0],true) or in_array($value,$arr_cls[1],true) or in_array($value,$arr_cls[2],true)){
        $part_t2_2[]=$value;
    }
    
}
3

There are 3 best solutions below

2
sensor On

is that what you mean? you can also use the count and for functions

$arra2=$arr_cls;
$arra1=$part_t2_2;
$final_array=null;
foreach($arra2 as $no => $arra22)
{
    foreach($arra22 as $el22)
    {
        foreach($arra1 as $el1)
        {
             if($el22===$el1)
             {
                 $final_array[]=$el22;
             }
        }
    }
}
var_dump($final_array);
1
Ken Lee On

One of the ways is to flatten the nested array first, then use in_array and a loop to do the job

We may use this function to flatten the nested array

function flatten(array $array) {
    $return = array();
    array_walk_recursive($array, function($a) use (&$return) { $return[] = $a; });
    return $return;
}

then loop over it, use in_array to check and build the $part_t2_2

foreach($part_t2 as $value){
   if (in_array($value, $arr_cls_new)) { 
        array_push($part_t2_2, $value);
   }
}

The whole, complete code will be

<?php

function flatten(array $array) {
    $return = array();
    array_walk_recursive($array, function($a) use (&$return) { $return[] = $a; });
    return $return;
}

$array1=array(
   "Paris SG", 
   "Brest", 
   "Monaco", 
   "Nizza", 
   "Lilla", 
   "Lens");
 
$array2=array(
   "Marsiglia", 
   "Rennes", 
   "Reims", 
   "Lione", 
   "Tolosa", 
   "Strasburgo");

$array3=array(
   "Le Havre", 
   "Montpellier", 
   "Lorient", 
   "Nantes", 
   "Metz", 
   "Clermont");

$arr_cls=array();

// build arr_cls below
array_push($arr_cls, $array1, $array2, $array3); 

// flatten array now
$arr_cls_new=flatten($arr_cls); 

//build part_t2 below

$part_t2=array(
   "Sturm Graz", 
   "Rennes", 
   "Sturm Graz", 
   "Reims", 
   "Tolosa", 
   "Le Havre", 
   "Paris SG", 
   "Lione", 
   "Clermont", 
   "Montpellier", 
   "Lorient", 
   "Strasburgo");

//declare a blank array

$part_t2_2=array();

// loop over it and only if an element in part_t2 exists in arr_cls_new (flattend array), put into part_t2_2

foreach($part_t2 as $value){
   if (in_array($value, $arr_cls_new)) { 
        array_push($part_t2_2, $value);
   }
}

var_dump($part_t2_2);

See Demo

1
mickmackusa On

Use array_diff() to see which values in the second array are not found in any of the first subarrays.

If you want to remove these outliers from the second array, you can use the first diff result to filter the original second array values.

Code: (Demo)

$array1 = [
    ["Paris SG", "Brest", "Monaco", "Nizza", "Lilla", "Lens"],
    ["Marsiglia", "Rennes", "Reims", "Lione", "Tolosa", "Strasburgo"],
    ["Le Havre", "Montpellier", "Lorient", "Nantes", "Metz", "Clermont"],
];

$array2 = [
    "Sturm Graz", 
    "Rennes", 
    "Sturm Graz", 
    "Reims", 
    "Tolosa", 
    "Le Havre", 
    "Paris SG", 
    "Lione", 
    "Clermont", 
    "Montpellier", 
    "Lorient", 
    "Strasburgo"
];

var_export(array_diff($array2, ...$array1));

echo "\n---\n";

var_export(array_diff_key($array2, array_diff($array2, ...$array1)));

Output:

array (
  0 => 'Sturm Graz',
  2 => 'Sturm Graz',
)
---
array (
  1 => 'Rennes',
  3 => 'Reims',
  4 => 'Tolosa',
  5 => 'Le Havre',
  6 => 'Paris SG',
  7 => 'Lione',
  8 => 'Clermont',
  9 => 'Montpellier',
  10 => 'Lorient',
  11 => 'Strasburgo',
)

If you want to make one-for-one eliminations then use manual looping and searching to populate the result array and reduce the second array as matches are found. In my Demo, I'll add a Sturm Graz to the first array to clarify the behavior.

$result = [];
foreach ($array1 as $row) {
    foreach ($row as $val) {
        $i = array_search($val, $array2);
        if ($i !== false) {
            unset($array2[$i]);
            $result[] = $val;
        }
    }
}
var_export($array2);

echo "\n---\n";

var_export($result);