-2

I have a the following array structure how can I process it in such a way so as to remove all the successive occurrences of change_status. Please see the array below and then what I want to achieve.

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

After processing the above array I would like to have the following

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

Any ideas will be appreciated. I do have a code that deals with the one dimensional array but this one is confusing me and I am a bit slow when dealing with the arrays.

4
  • 3
    You asked this question not long ago. What happened to answer here? Commented Jul 18, 2013 at 9:04
  • Oh didn't look at that thanks a lot for your help guys Commented Jul 18, 2013 at 9:10
  • this is not really a "array remove duplicate"... cause he want to remove duplicate entry with a logic... removing all the duplicates the expected results will be an array of 2 items, and not what he want. Commented Jul 18, 2013 at 9:16
  • Hi @Chibueze Opata the other function worked but i want another function that can work on the multidimensional arrays Commented Jul 18, 2013 at 9:26

3 Answers 3

2

Iterate over the array while keeping track of the previous element. If the current element is equal to the previous, remove it:

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

Note that I have chosen to use the identity operator === to compare the previous and current value because this looks the most fitting for the data you give. If the data format can vary you would need to use a looser form of comparison.

Sign up to request clarification or add additional context in comments.

14 Comments

will this really remove all duplicates? or only those which goes in a row?
I think you missed the line to assign value to $previous in loop
@insanebits: Those in a row, as the question says "successive duplicates".
Oh sorry missed this one :)
Thank you very much Jon :-) worked like a charm :) any ideas about how to do this using recursion?
|
0

Please check with following code

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

Comments

0

Of course you can use the object and array_unique for this kind of task:

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

And for each "start" key you can increment your group iterator.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.