skip to Main Content

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

2

Answers


  1. 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);
    
    Login or Signup to reply.
  2. 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);
       }
    }
    

    So, 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search