繁体   English   中英

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

[英]How to remove all values from an array before a specific value

我有以下数组,我需要删除键 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 是上面的例子

这将返回相同的数组

一条短线...

$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);

function array_search搜索BEGIN:VEVENT的第一次出现并返回找到的偏移量,该偏移量使用array_values识别原始数组的值。 您可以将此键用作array_slice function 的偏移量,它返回数组的 rest。

如果给定数组中不存在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"
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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