-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)
*/
}
}
forgot that detail, I would like to make a condition ex: if find the number 2,3 in the array first it turns null
– David
Exactly as you explained, I would like it to return: array(1, null, null)
– David
right... what have you tried to do since?
– Daniel Omine
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; }
– David
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;
– Daniel Omine
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– David
I tested the
$arr_prim[$k] = null;
but did the opposite– David
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
– Daniel Omine
edited the question the result should be
array(1, null, null)
but it wasarray(null, 2, 3)
– David
sorry, but I voted to close "how not clear enough"... I tried to help but you seem scattered in the explanations.
– Daniel Omine
even so thank you very much! It helped a lot.
– David
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
– David
Take a tour : http://answall.com/tour
– Daniel Omine