为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
慕丝7291255 2023-04-28 16:01:08
我有以下数组,我需要删除键 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 是上面的例子这将返回相同的数组
查看完整描述

1 回答

?
慕容3067478

TA贡献1773条经验 获得超3个赞

一个短的班轮...


$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"

}


查看完整回答
反对 回复 2023-04-28
  • 1 回答
  • 0 关注
  • 104 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信