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

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

提问人:A.V. 提问时间:6/4/2020 最后编辑:A.V. 更新时间:6/4/2020 访问量:55

问:

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

这将返回相同的数组

PHP 数组 切片

评论

0赞 Marcel 6/4/2020
到目前为止,您尝试了什么,您在执行此任务时遇到的问题在哪里?请记住:SO 不是一项编码服务,您可以在其中结识正在做您工作的人。向我们展示一些代码,我们将尽力为您提供帮助。
0赞 A.V. 6/4/2020
我尝试了以下代码,但没有成功: / / 找到你要找的键的位置。$position = array_search(BEGIN:VEVENT, array_values($array));如果找到位置,则拼接数组。if ($position !== false) { array_splice($array, ($position - 1)$array var_dump);其中 $array 是上面的示例数组 如您所知,我想在 BEGIN:VEVENT 之前清除所有 DTSTART,因为 DTSTART 之前,该值不是我需要存储在 mysql 中的信息。
0赞 Marcel 6/4/2020
由于这不是您在 SO 上的第一个问题,因此下次尝试添加您迄今为止为自己尝试过的内容。我已经编辑了你的问题。

答:

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
那你就做错了什么。编辑您的问题并展示您的代码。