Anthony Pillos
Anthony Pillos

Reputation: 205

How to Remove the First Occurrence of a duplicate Value in Array using PHP

I have a group of an array

Array ( 
[0] => 0
[1] => 1
[2] => 1 
[3] => -1 
[4] => 3 
[5] => 0 
[6] => 2 
[7] => 3
[8] => -2 
[9] => 2 
[10] => 0 
[11] => 1
[12] => 1 
[13] => 2 
[14] => 5 
[15] => 0 ) 

And i want to delete the values of this array:

Array ( 
[0] => 3 
[1] => 1 
[2] => 2 
[3] => -1 
)

But i want to preserve some of the duplicate data.. I just want to delete one occurence of the second array..

The Result would be like this: EXPECTED RESULT

Array ( 
[0] => 0
[2] => 1 
[4] => 3 
[5] => 0 
[6] => 2 
[8] => -2 
[10] => 0 
[11] => 1
[12] => 1 
[13] => 2 
[14] => 5 
[15] => 0 ) 

How can i do that?.. array_unique and array_diff is removing all the occurence of the value in the first array..

Upvotes: 2

Views: 3674

Answers (7)

Yeti
Yeti

Reputation: 2855

A more general approach - without using the lame unset function - would be:

foreach($values as $v)
{
    array_splice($array, array_search($v, $array), 1);
}

This removes the first occurrence of each value in $values from your $array.

An example-code in PHP:

$array = array("a","a","b","c","b","c","d","d","e","d");
$values = array("b","c","f");

array_walk($values, function(&$v,$k) use(&$array){array_splice($array, array_search($v, $array), 1);});

echo implode(",", $array);

Will output:

a,b,c,d,d,e,d

Upvotes: 0

Yam Mesicka
Yam Mesicka

Reputation: 6601

Well, try the following (Your script should define the $first_array and $second_array variables):

foreach ($second_array as $to_delete) {
      $key = array_search($to_delete, $first_array);
      if ($key !== FALSE) {
          unset($first_array[$key]);
      }
}

Upvotes: 2

Francis Avila
Francis Avila

Reputation: 31641

This approach should run faster than the array_search() or in_array() approaches because it avoids a linear search through the array on each iteration:

function del_first_found($array, $todel) {
    $todel = array_fill_keys($todel, true);
    $newarray = array();
    foreach ($array as $key => $value) {
        if (isset($todel[$value])) {
            unset($todel[$value]);
        } else {
            $newarray[$key] = $value;
        }
    }
    return $newarray;
}

$input = array(0,1,1,-1,3,0,2,3,-2,2,0,1,1,2,5,0);
$to_delete = array(3,1,2,-1);
$result = del_first_found($input, $to_delete);
var_export($result);

Upvotes: 0

Chvanikoff
Chvanikoff

Reputation: 1329

$array = array(0,1,1,-1,3,0,2,3,-2,2,0,1,1,2,5,0);
$delete = array(3,1,2,-1);
$result = array();
foreach ($array as $val) {
    if (in_array($val, $delete) && in_array($val, $result)) {
        unset($delete[$val]);
    } else {
        $result[] = $val;
    }
}

Result:

Array
(
    [0] => 0
    [1] => 1
    [2] => -1
    [3] => 3
    [4] => 0
    [5] => 2
    [6] => -2
    [7] => 0
    [8] => 1
    [9] => 1
    [10] => 2
    [11] => 5
    [12] => 0
)

P.S.: if you want to delete an element of $delete array even if there's only 1 occurence of it then replace

if (in_array($val, $delete) && in_array($val, $result)) {

with

if (in_array($val, $delete)) {

Upvotes: 0

echo
echo

Reputation: 7875

Another one

<?php
$source = array(0, 1, 1, -1, 3, 0, 2, 3, -2, 2, 0, 1, 1, 2, 5, 0); 
$items_to_filter = array(3, 1, 2, -1);
$result = array_filter($source, function ($var) {
                global $items_to_filter;
                $index = array_search($var, $items_to_filter);
                if($index !== FALSE)
                {   
                        unset($items_to_filter[$index]);
                        return false;
                }   
                return true;
          }); 
?>

Upvotes: 1

Php Geek
Php Geek

Reputation: 1107

array2[i] is the value from array2 that you want to delete

$value = array_search($array2[i],$array1);
if($value)
{
    unset($value);
}

Upvotes: -1

shapeshifter
shapeshifter

Reputation: 2967

What about something like,

for($i = 0; $i < count($a1); $i++)
{
  if(in_array($a1[$i], $a2)) {
    for($j=0; $j<count($a2); $j++)
      if($a2[$j] == $a1[$i])
        unset($a2[$j]);

    unset($a1[$i]);
  }
}

I haven't tested this so there maybe errors.

Upvotes: 0

Related Questions