Compare the similarity of values of two arrays is possible? [closed]

-1

string of the first array $arr_prim[] should be null when it looks similar to the second $arr_sec[] ,

 <?php
    $arr_prim[] = array('2','3');

 //array primaria

    $arr_sec[] = array('1','2','3');

  //array secundaria


   foreach($arr_prim as & $key):
     var_dump($key); 
   endforeach;

   foreach($arr_sec as & $res):
   var_dump($res);
   endforeach; 
   ?>
      /*Ambos irá imprimir 
   array (size=3)
  0 => string '1' (length=1)
  1 => string '2' (length=1)
  2 => string '3' (length=1)
   */     

   foreach($arr_prim as $k => $value){
        if($s = array_search($value, $arr_sec)){

        $arr_sec[$k] = null;
        var_dump($arr_sec);

    /*resultado da impressão:
    array (size=3)
        0 => null
        1 => string '2' (length=1)
        2 => string '3' (length=1)
    */

        }

    }
    
asked by anonymous 28.06.2016 / 06:02

1 answer

1

The issue lacks further details.

Regardless of this I made a simple loopback where it checks whether the value of an array exists in the other:

$arr_prim = array('1','2','3');
$arr_sec = array('2','3');

foreach ($arr_sec as $k => $v) {
    if ($p = array_search($v, $arr_prim)) {
        echo 'encontrou valor '.$v.' no array primário, na posição '.$p.PHP_EOL.'<br>';
        /*
        Setar valor nulo?
        quem deve receber valor nulo? array primario ou secundario? o array inteiro ou somente a posição onde foi encontrado?
        Se a poição não existir, deve ser criada e setada como nulo?
        */

        $arr_prim[$k] = null;
    }
}
  

ex if the number 2,3 of the first array is similar to the 2,3 of the second   array, I want it to be null

In this section it is not clear if you are talking about positions / indexes / keys or values. Example,

The primary and secondary array have values 2 and 3. This means that the primary array should look like this:

array(1, null, null)

Another way of interpreting the question is based on the indexes and not only on the values. But I'd rather not write more codes until it's clear what needs to be done.

    
28.06.2016 / 10:20