2

我有一组数组

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 ) 

我想删除这个数组的值:

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

但我想保留一些重复的数据..我只想删除第二个数组的一个出现..

结果将是这样的: 预期结果

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 ) 

我该怎么做?.. array_unique 和 array_diff 正在删除第一个数组中所有出现的值..

4

7 回答 7

2

好吧,试试下面的(你的脚本应该定义 $first_array 和 $second_array 变量):

foreach ($second_array as $to_delete) {
      $key = array_search($to_delete, $first_array);
      if ($key !== FALSE) {
          unset($first_array[$key]);
      }
}
于 2013-01-09T04:59:36.293 回答
1

另一个

<?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;
          }); 
?>
于 2013-01-09T05:15:05.167 回答
0
$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;
    }
}

结果:

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
)

PS:如果你想删除 $delete 数组的一个元素,即使它只有 1 次出现,然后替换

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

if (in_array($val, $delete)) {
于 2013-01-09T05:11:43.100 回答
0

怎么样的东西,

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

我没有对此进行测试,因此可能存在错误。

于 2013-01-09T04:56:55.133 回答
0

这种方法应该比array_search()orin_array()方法运行得更快,因为它避免了在每次迭代时对数组进行线性搜索:

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);
于 2013-01-09T05:23:47.040 回答
0

一种更通用的方法 - 不使用 lame unset 函数 - 将是:

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

这会$values从您的$array.

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

将输出:

a,b,c,d,d,e,d
于 2013-09-21T22:22:13.110 回答
-1

array2[i] 是要删除的 array2 中的值

$value = array_search($array2[i],$array1);
if($value)
{
    unset($value);
}
于 2013-01-09T05:04:55.860 回答