简体   繁体   中英

Eliminate consecutive values from multi dimentional array in PHP

$ticket_details =  Array ( 
    18271 => Array ( 
        0 => Array ( 
            0 => 'Aziz',
            1 => '2010-10-20 08:12:53' 
        ),
        1 => Array ( 
            0 => 'Aziz' ,
            1 => '2010-10-20 10:10:10' 
        ),
        2 => Array ( 
            0 => 'Aziz',
            1 => '2010-10-20 11:54:57' 
        ) ,
        3 => Array ( 
            0 => 'gemma' ,
            1 => '2010-10-20 23:47:41' 
        ) ,
        4 => Array ( 
            0 => 'gemma' ,
            1 => '2010-11-01 08:40:20' 
        ) ,
        5 => Array ( 
            0 => 'Aziz' ,
            1 => '2010-11-03 10:00:44' 
        ) ,
        6 => Array ( 
            0 => 'Aziz' ,
            1 => '2010-11-03 11:21:55' 
        )  ,
        7 => Array ( 
            0 => 'Aziz' ,
            1 => '2010-11-03 11:21:55' 
        )  ,
        8 => Array ( 
            0 => 'gemma' ,
            1 => '2010-11-03 11:21:55' 
        )  ,
        9 => Array ( 
            0 => 'Aziz' ,
            1 => '2010-11-03 11:21:55' 
        ) 
    ) 
);

I want to remove consecutive values by 0th element. Suppose: 'Aziz' coming 3 times consecutively. I want to keep any one of them. Again 'gemma' is coming 2 times consecutively. I want to take any one of these two.

The output should come like this:

$ticket_details =  Array ( 
    18271 => Array ( 
        0 => Array ( 
            0 => 'Aziz',
            1 => '2010-10-20 08:12:53' 
        ),
        1 => Array ( 
            0 => 'gemma' ,
            1 => '2010-11-01 08:40:20' 
        ) ,
        2 => Array ( 
            0 => 'Aziz' ,
            1 => '2010-11-03 11:21:55' 
        )  ,
        3 => Array ( 
            0 => 'gemma' ,
            1 => '2010-11-03 11:21:55' 
        )  ,
        4 => Array ( 
            0 => 'Aziz' ,
            1 => '2010-11-03 11:21:55' 
        ) 
    ) 
);

I tried these codes. But it's working only for first consecutive values. Not for other values.

    foreach ($ticket_details[18271] as $key => $value) {
            if (isset($ticket_details[18271][$key+1])) {

                if ($ticket_details[18271][$key][0] == $ticket_details[18271][$key+1][0]) {
                    $temp[$y] = $ticket_details[18271][$key+1][0];                      
                } else {
                    $temp[$y] = $ticket_details[18271][$key+1][0];
                }
                if (isset($temp[$y-1]) && (($ticket_details[18271][$key-1][0] == $ticket_details[18271][$key][0]) && 
                    ($ticket_details[18271][$key][0] == $ticket_details[18271][$key+1][0]))) {
                    $y = $y-1;
                    $temp[$y] = $ticket_details[18271][$key+1][0];
                }
            }

            $y++;
        }


        print_r($temp);

Just keep track of the last name used.

$temp = array();
$last_name_stored=''; // To keep track of last saved item
foreach ($ticket_details[18271] as $key => $value) {
    if ($value[0] != $last_name_stored){ // check if already used
        $temp[18271][] = $value;
        $last_name_stored = $value[0]; // update the last saved item
    }
}

print_r($temp);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM