Is comparing similarity of values of two arrays possible?

Asked

Viewed 296 times

-1

string of the first array$arr_prim[] shall be null when it is 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)
    */

        }

    }

1 answer

1

The question lacks more details.

Regardless of that I made a simple repetition loop where it checks if the value of one 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 whether you are talking about positions/indexes/keys or values. Example,

The primary and secondary array have value 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 indexes and not only on values. But I’d rather not write more code until it’s clear what needs to be done.

  • forgot that detail, I would like to make a condition ex: if find the number 2,3 in the array first it turns null

  • Exactly as you explained, I would like it to return: array(1, null, null)

  • right... what have you tried to do since?

  • I tried to do the following @Daniel Omine create a new array $arr = array('num' => $value); following I made a condition, if(in_array('num', $value)){ $value['num'] = null; }

  • That’s almost it. When assigning the null value, specify the current loop key. I edited the answer now that it’s clearer. I just added the excerpt $arr_prim[$k] = null;

  • the idea is based on following users, e.g.: I am following users of $arr_prim if they exist in the $arr_sec it will be null as I no longer need to rescue the user id followed

  • I tested the $arr_prim[$k] = null; but did the opposite

  • but wasn’t the primary array to receive null values when finding repeated values in the secondary array? And about the goal of "following users", maybe if we explain the goal, we can come up with a completely different and possibly better solution

  • edited the question the result should be array(1, null, null) but it was array(null, 2, 3)

  • sorry, but I voted to close "how not clear enough"... I tried to help but you seem scattered in the explanations.

  • even so thank you very much! It helped a lot.

  • not being boring I am looking for solution and until today nothing, I would like the result to be exactly: array(1, null, null), I used the in_array , array_search and was not successful

  • Take a tour : http://answall.com/tour

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.