提问人:A.V. 提问时间:6/4/2020 最后编辑:A.V. 更新时间:6/4/2020 访问量:55
如何从数组中删除特定值之前的所有值
How to remove all values from an array before a specific value
问:
我有以下数组,我需要删除值为 BEGIN:VEVENT 的键 83 之前的所有值。我需要的不是按键执行此操作,而是仅使用值。
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:[email protected] [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:[email protected] [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 是上面的例子
这将返回相同的数组
答:
0赞
Marcel
6/4/2020
#1
一个短的衬里......
$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:[email protected]',
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:[email protected]',
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搜索第一次出现的数组,并返回找到的偏移量,以识别带有array_values的原始数组的值。此键可用作 array_slice 函数的偏移量,该函数返回数组的其余部分。BEGIN:VEVENT
如果给定数组中不存在,则将返回整个数组。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:[email protected]"
[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:[email protected]"
[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"
}
评论
0赞
A.V.
6/4/2020
我尝试了您的代码,这工作正常,但在我的数组中它不起作用。这正是我想做的,我需要删除 BEGIN:VEEVENT 值之前的所有键值。
0赞
Marcel
6/4/2020
所有密钥都被移除了。作为结果的新数组以键 0 开头。所以你想把钥匙从针的位置移开吗?也许更好,当你编辑你的问题并举个例子时,最终的数组应该是什么样子?array_slice
0赞
A.V.
6/4/2020
我想在 BEGIN:VEVENT 出现后保留所有值,并在此之前删除所有值。因此,新数组开始 0=> BEGIN:VEVENT 并继续下一个值。
0赞
A.V.
6/4/2020
我理解,因为我在我的服务器中测试了您的代码并且工作正常。但是当我尝试在我的数组中执行此操作时,它不起作用。它返回与之前切片相同的数组。
0赞
Marcel
6/4/2020
那你就做错了什么。编辑您的问题并展示您的代码。
上一个:在单击之前阻止加载图像
评论