-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.

Abbasi
  • 125
  • 7
  • 3
    You asked this question not long ago. [What happened to answer here?](http://stackoverflow.com/a/17712070/612717) – Chibueze Opata Jul 18 '13 at 09:04
  • Oh didn't look at that thanks a lot for your help guys – Abbasi Jul 18 '13 at 09: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. – MiPnamic Jul 18 '13 at 09:16
  • Hi @Chibueze Opata the other function worked but i want another function that can work on the multidimensional arrays – Abbasi Jul 18 '13 at 09:26

3 Answers3

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.

Jon
  • 428,835
  • 81
  • 738
  • 806
  • will this really remove all duplicates? or only those which goes in a row? – insanebits Jul 18 '13 at 09:07
  • 1
    I think you missed the line to assign value to `$previous` in loop – Prasanth Bendra Jul 18 '13 at 09:07
  • @insanebits: Those in a row, as the question says "successive duplicates". – Jon Jul 18 '13 at 09:07
  • Oh sorry missed this one :) – insanebits Jul 18 '13 at 09:08
  • Thank you very much Jon :-) worked like a charm :) any ideas about how to do this using recursion? – Abbasi Jul 18 '13 at 09:08
  • Actually $previous will save only the value of the one previous, means, it won't remove those who are multiple time there, since it will look for the previous which will never be the same or in rare cases and won't check for others cases. – mike27015 Jul 18 '13 at 09:09
  • @PrasanthBendra: It's right there... – Jon Jul 18 '13 at 09:10
  • @WajidAbbasi: It's not clear what "using recursion" means. Specify things more precisely. – Jon Jul 18 '13 at 09:11
  • @Jon : Yeah it is there now, It was not there before :) – Prasanth Bendra Jul 18 '13 at 09:11
  • @mike27015 see third comment – pulsar Jul 18 '13 at 09:12
  • @PrasanthBendra: I forgot it initially, but I put it in seconds after posting the answer. Your comment was quite a bit later, so I thought you 'd have seen it directly. Sorry. – Jon Jul 18 '13 at 09:13
  • @Jon : No prob...I just pointed it...Actually it is the problem with my internet it is very slow :D – Prasanth Bendra Jul 18 '13 at 09:15
  • Hi Jon using recursion means any recursion function where i can pass an array and then decide to make a decision based on the value in a row not the entire row for example if the above array is to become "1" => array( "status_change" => "stop", "clock_status" => "2", "number"=>"1" ),"1" => array( "status_change" => "stop", "clock_status" => "2", "number"=>"2" ),"1" => array( "status_change" => "stop", "clock_status" => "2", "number"=>"3" )); then i want to make decision based on the status_change rather than the whole row – Abbasi Jul 18 '13 at 09:23
  • @WajidAbbasi: That's not recursion. Simply replace `===` with an different check. – Jon Jul 18 '13 at 09:29
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.

Serge Velikan
  • 1,131
  • 15
  • 31
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 );
Loken Makwana
  • 3,788
  • 2
  • 21
  • 14