-2

我有一个以下数组结构,我该如何处理它以删除所有连续出现的 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" )
    );

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

4

3 回答 3

2

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

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

请注意,我选择使用恒等运算符===来比较先前值和当前值,因为这看起来最适合您提供的数据。如果数据格式可以变化,您将需要使用更宽松的比较形式。

于 2013-07-18T09:02:32.667 回答
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);
?>

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

于 2013-07-18T09:09:55.907 回答
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 );
于 2013-07-18T09:13:48.750 回答