$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'
)
)
);
我想删除第0个元素的连续值。 假设:'Aziz'连续3次出现。我想保留其中任何一个。 'gemma'再次连续出现2次。我想采取这两个中的任何一个。
输出应该是这样的:
$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'
)
)
);
我试过这些代码。但它仅适用于第一个连续值。不适用于其他价值观。
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);
答案 0 :(得分:2)
只需跟踪使用的姓氏。
$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);