从数组PHP ARRAY

时间:2016-12-15 08:01:35

标签: php

我有以下数组

Array ( [0] => **start** [1] => **start** [2] => name [3] => producer [4] => contact name [5] => 03354222271 [6] => fzahid001@gmail.com [7] => day contact name [8] => 03354222271 [9] => venue name [10] => adress [11] => country [12] => city [13] => desc [14] => file [15] => 2016-01-01 [16] => 01:00 [17] => 06:00 [18] => 2 [19] => music_festival [20] => 2000+ [21] => quick [22] => alcohol [23] => quick [24] => 10x10 [25] => ***no*** [26] => ***no*** [27] => ***no*** [28] => 2 [29] => 0 [30] => 4 [31] => $158 [32] => $118.5 [33] => $284 [34] => $960 [35] => **start** [36] => na [37] => producer [38] => con [39] => 1 [40] => fzahid001@gmail.com [41] => nam [42] => 1 [43] => venue [44] => ad [45] => co [46] => ci [47] => description test [48] => download555ssss.png [49] => 2016-12-07 [50] => 13:00 [51] => 19:00 [52] => 2 [53] => manual_selection [54] => ATV [55] => 10x10 [56] => no [57] => no [58] => no [59] => 1 [60] => 1 [61] => 1 [62] => $109.5 [63] => $118.5 [64] => $99.5 [65] => $728 [66] => **start** [67] => Race [68] => Race Club [69] => Faizan [70] => 03354222271 [71] => fzahid001@gmail.com [72] => Faizan Zahid [73] => 03354222271 [74] => DHA [75] => 90-H Tariq Gardens [76] => Pakistan [77] => Lahore [78] => [79] => images.jpg [80] => 2017-01-01 [81] => 00:00 [82] => 07:00 [83] => 2 [84] => cycling_road_race [85] => 1_field [86] => 1_399 [87] => electronic [88] => quick [89] => 10x10 [90] => no [91] => no [92] => no [93] => 1 [94] => 0 [95] => 1 [96] => $109.5 [97] => $118.5 [98] => $99.5 [99] => $796.5 [100] => ) 

现在我要做的是,删除“start”的连续重复项。我目前正在使用以下代码

foreach ($updateddata as $value){
    if($value != $previousvalue){

        array_push($finaldata, $value);
    }
    $previousvalue  = $value;
}

但也删除了我不想删除的其他连续重复项。请帮我怎么做。

我已经突出显示了我要删除的“开始”的出现并使其成为一个单独的条目,同时保留其他字符串,例如我不想删除字符串“no”

的重复项

2 个答案:

答案 0 :(得分:1)

试试这个

if($value != $previousvalue){    
     array_push($finaldata, $value);
} else if( $value != 'start'){
     array_push($finaldata, $value);
}

答案 1 :(得分:0)

要删除"start"值的连续重复项,您应将关键条件限制为该值。
使用以下方法:

// exemplary array
$updateddata = [
    'start', 'start', 'producer', 'contact', 'no', 'no'
];

foreach ($updateddata as $k => $v) {
    if (isset($updateddata[$k-1]) && $v == 'start' && $v == $updateddata[$k-1]) {
        unset($updateddata[$k]);
    }
}

输出:

Array
(
    [0] => start
    [2] => producer
    [3] => contact
    [4] => no
    [5] => no
)