如何从特定值之前的数组中删除所有值

我有以下数组,我需要删除键 83 之前的所有值,值为 BEGIN:VEVENT。我需要的不是按键,而是仅使用值。

   Array ( [75] => END:DAYLIGHT [76] => BEGIN:STANDARD [77] => DTSTART:20211031T030000 [78] => TZOFFSETFROM:+0300 [79] => TZOFFSETTO:+0200 [80] => TZNAME:EET [81] => END:STANDARD [82] => END:VTIMEZONE [83] => BEGIN:VEVENT [84] => SUMMARY: [85] => DESCRIPTION: Tourist Agent Office: First Name: Last Name: Email: xxx@gma [86] => il.com Visitors: 1 Phone: Details: Time Slots: 12:00 μμ - 11:59 πμ [87] => [88] => DTSTART:20200711T120001 [89] => DTEND:20200725T115902 [90] => UID:2020-07-11 12:00:01_25@demo.icalendar.org [91] => DTSTAMP:20200604T130218 [92] => CREATED:20200129T104306 [93] => LAST-MODIFIED:20200129T104306 [94] => STATUS:CONFIRMED [95] => END:VEVENT [96] => BEGIN:VEVENT [97] => SUMMARY: [98] => DESCRIPTION: Tourist Agent Office: First Name: Last Name: Email: xxx@gma [99] => il.com Visitors: 1 Phone: Details: Time Slots: 12:00 μμ - 11:59 πμ [100] => [101] => DTSTART:20200912T120001 [102] => DTEND:20200926T115902 [103] => UID:2020-09-12 12:00:01_26@demo.icalendar.org [104] => DTSTAMP:20200604T130218 [105] => CREATED:20200203T060059 [106] => LAST-MODIFIED:20200203T060059 [107] => STATUS:CONFIRMED [108] => END:VEVENT [109] => END:VCALENDAR [110] =>)

到目前为止我已经尝试过...

$result = array_slice($array, array_search('BEGIN:VEVENT', $array) ?: 0);
print_r($array);

其中 $array 是上面的例子

这将返回相同的数组


慕丝7291255
浏览 105回答 1
1回答

慕容3067478

一个短的班轮...$data = [    75 => 'END:DAYLIGHT',    76 => 'BEGIN:STANDARD',     77 => 'DTSTART:20211031T030000',    78 => 'TZOFFSETFROM:+0300',     79 => 'TZOFFSETTO:+0200',     80 => 'TZNAME:EET',     81 => 'END:STANDARD',    82 => 'END:VTIMEZONE',    83 => 'BEGIN:VEVENT',    84 => 'SUMMARY:',    85 => 'DESCRIPTION: Tourist Agent Office: First Name: Last Name: Email: xxx@gma',    86 => 'il.com Visitors: 1 Phone: Details: Time Slots: 12:00 μμ - 11:59 πμ',     87 => '',    88 => 'DTSTART:20200711T120001',    89 => 'DTEND:20200725T115902',     90 => 'UID:2020-07-11 12:00:01_25@demo.icalendar.org',     91 => 'DTSTAMP:20200604T130218',    92 => 'CREATED:20200129T104306',     93 => 'LAST-MODIFIED:20200129T104306',    94 => 'STATUS:CONFIRMED',    95 => 'END:VEVENT',     96 => 'BEGIN:VEVENT',     97 => 'SUMMARY:',    98 => 'DESCRIPTION: Tourist Agent Office: First Name: Last Name: Email: xxx@gma',     99 => 'il.com Visitors: 1 Phone: Details: Time Slots: 12:00 μμ - 11:59 πμ',    100 => '',    101 => 'DTSTART:20200912T120001',    102 => 'DTEND:20200926T115902',    103 => 'UID:2020-09-12 12:00:01_26@demo.icalendar.org',    104 => 'DTSTAMP:20200604T130218',     105 => 'CREATED:20200203T060059',    106 => 'LAST-MODIFIED:20200203T060059',    107 => 'STATUS:CONFIRMED',    108 => 'END:VEVENT',    109 => 'END:VCALENDAR',];$result = array_slice($data, array_search('BEGIN:VEVENT', array_values($data)) ?: 0);var_dump($result);函数array_search搜索 的第一次出现BEGIN:VEVENT并返回找到的偏移量,用array_values识别原始数组的值。您可以将此键用作array_slice函数的偏移量,该函数返回数组的其余部分。如果BEGIN:VEVENT给定数组中不存在,则将返回整个数组。上述示例的结果:array(27) {    [0] => string(12) "BEGIN:VEVENT"    [1] => string(8) "SUMMARY:"    [2] => string(72) "DESCRIPTION: Tourist Agent Office: First Name: Last Name: Email: xxx@gma"    [3] => string(70) "il.com Visitors: 1 Phone: Details: Time Slots: 12:00 μμ - 11:59 πμ"    [4] => string(0) ""    [5] => string(23) "DTSTART:20200711T120001"    [6] => string(21) "DTEND:20200725T115902"    [7] => string(45) "UID:2020-07-11 12:00:01_25@demo.icalendar.org"    [8] => string(23) "DTSTAMP:20200604T130218"    [9] => string(23) "CREATED:20200129T104306"    [10] => string(29) "LAST-MODIFIED:20200129T104306"    [11] => string(16) "STATUS:CONFIRMED"    [12] => string(10) "END:VEVENT"    [13] => string(12) "BEGIN:VEVENT"    [14] => string(8) "SUMMARY:"    [15] => string(72) "DESCRIPTION: Tourist Agent Office: First Name: Last Name: Email: xxx@gma"    [16] => string(70) "il.com Visitors: 1 Phone: Details: Time Slots: 12:00 μμ - 11:59 πμ"    [17] => string(0) ""    [18] => string(23) "DTSTART:20200912T120001"    [19] => string(21) "DTEND:20200926T115902"    [20] => string(45) "UID:2020-09-12 12:00:01_26@demo.icalendar.org"    [21] => string(23) "DTSTAMP:20200604T130218"    [22] => string(23) "CREATED:20200203T060059"    [23] => string(29) "LAST-MODIFIED:20200203T060059"    [24] => string(16) "STATUS:CONFIRMED"    [25] => string(10) "END:VEVENT"    [26] => string(13) "END:VCALENDAR"}
打开App,查看更多内容
随时随地看视频慕课网APP