从多维php数组中删除重复条目

时间:2013-07-18 09:00:20

标签: php arrays

我有一个以下数组结构,我如何以这种方式处理它,以便删除所有连续出现的change_status。请参阅下面的数组,然后看看我想要实现的目标。

$a=array( 
    "0" => array(  "status_change" => "start", "clock_status" => "1" ),
    "1" => array(  "status_change" => "stop", "clock_status" => "2" ),
    "2" => array(  "status_change" => "stop", "clock_status" => "2" ),
    "3" => array(  "status_change" => "stop", "clock_status" => "2" ),
    "4" => array(  "status_change" => "stop", "clock_status" => "2" ),
    "5" => array(  "status_change" => "stop", "clock_status" => "2" ),
    "6" => array(  "status_change" => "stop", "clock_status" => "2" ),
    "7" => array(  "status_change" => "start", "clock_status" => "1" ),
    "8" => array(  "status_change" => "stop", "clock_status" => "2" ),
    "9" => array(  "status_change" => "start", "clock_status" => "1" ),
    "10" => array(  "status_change" => "start", "clock_status" => "1" ),
    "11" => array(  "status_change" => "start", "clock_status" => "1" ),
    "12" => array(  "status_change" => "start", "clock_status" => "1" )
    );

处理完上面的数组后,我希望有以下内容

$a=array( 
    "0" => array(  "status_change" => "start", "clock_status" => "1" ),
    "1" => array(  "status_change" => "stop", "clock_status" => "2" ),
    "7" => array(  "status_change" => "start", "clock_status" => "1" ),
    "8" => array(  "status_change" => "stop", "clock_status" => "2" ),
    "9" => array(  "status_change" => "start", "clock_status" => "1" )
    );

任何想法将不胜感激。我有一个处理一维数组的代码,但是这个让我感到困惑,在处理数组时我有点慢。

3 个答案:

答案 0 :(得分:2)

在跟踪前一个元素的同时迭代数组。如果当前元素等于前一个元素,则将其删除:

$previous = null;
foreach ($a as $key => $value) {
    if ($value === $previous) {
        unset($a[$key]);
    }
    $previous = $value;
}

请注意,我已选择使用标识运算符===来比较先前值和当前值,因为这看起来最适合您提供的数据。如果数据格式可能有所不同,则需要使用更宽松的比较形式。

答案 1 :(得分:0)

当然,您可以将object和array_unique用于此类任务:

<?php
class Test
{
    public $key;
    public $value;
    private $group;
    public function __construct($k,$v,$group=0)
    {
        $this->key=$k;
        $this->value=$v;
            $this->group=$group;
    }
    public function __toString()
    {
        return$this->key."_".$this->value."_".$this->group;
    }
}
$arr=array();
$arr[]=new Test("start",1);
$arr[]=new Test("stop",2);
$arr[]=new Test("stop",2);
$arr[]=new Test("stop",2);

$arr=array_unique($arr);
print_r($arr);
?>

对于每个“开始”键,您可以增加组迭代器。

答案 2 :(得分:0)

请查看以下代码

$source=array( 
    "0" => array(  "status_change" => "start", "clock_status" => "1" ),
    "1" => array(  "status_change" => "stop", "clock_status" => "2" ),
    "2" => array(  "status_change" => "stop", "clock_status" => "2" ),
    "3" => array(  "status_change" => "stop", "clock_status" => "2" ),
    "4" => array(  "status_change" => "stop", "clock_status" => "2" ),
    "5" => array(  "status_change" => "stop", "clock_status" => "2" ),
    "6" => array(  "status_change" => "stop", "clock_status" => "2" ),
    "7" => array(  "status_change" => "start", "clock_status" => "1" ),
    "8" => array(  "status_change" => "stop", "clock_status" => "2" ),
    "9" => array(  "status_change" => "start", "clock_status" => "1" ),
    "10" => array(  "status_change" => "start", "clock_status" => "1" ),
    "11" => array(  "status_change" => "start", "clock_status" => "1" ),
    "12" => array(  "status_change" => "start", "clock_status" => "1" )
    );

function reduce_successive(&$source) {      
    $prev_status = 'none';
    foreach($source as $key=>$item) {
        if($item['status_change'] == $prev_status) {
            unset($source[$key]);
        } else {
            $prev_status = $item['status_change'];
        }
    }
}
reduce_successive($source);
var_dump( $source );