gpt4 book ai didi

php - 从数组中选择特定元素

转载 作者:可可西里 更新时间:2023-11-01 01:02:34 24 4
gpt4 key购买 nike

我正在尝试从数组中删除除 DTSTART 和 DTEND 之外的所有内容

所以我正在尝试转换它:

Array
(
[0] => Array
(
[DTEND] => 20130417
[DTSTART] => 20130403
[UID] => cerog90g7-fcqls51z0wteew@airbnb.com
[DESCRIPTION] => CHECKIN: 04/08/2012\nCHECKOUT: 04/17/2012\nNIGHTS: 14\nPHO
[NE] => +1 504 914 0591\nEMAIL: testhere@email.com\nPROPERTY: Breathtaking views over the lake\n
[SUMMARY] => Name here (ED5ASC)
[LOCATION] => Breathtaking views over the lake
)

[1] => Array
(
[DTEND] => 20131231
[DTSTART] => 20131226
[UID] => rtyrtyg90g7--t520wkwqv2i8@airbnb.com
[DESCRIPTION] => CHECKIN: 12/26/2013\nCHECKOUT: 12/31/2013\nNIGHTS: 5\nPHON
[E] => +44 7843 387767\nEMAIL: floraking@hotmail.com\nPROPERTY: Breathtaking views over the lake\n
[SUMMARY] => Name here (JrtyKW9)
[LOCATION] => Breathtaking views over the lake
)

[2] => Array
(
[DTEND] => 20121123
[DTSTART] => 20121117
[UID] => ntry5rxts23--qe9rtyrywi0@airbnb.com
[SUMMARY] => Not available
)

)

看起来像这样:

Array
(
[0] => Array
(
[DTEND] => 20130417
[DTSTART] => 20130403
)

[1] => Array
(
[DTEND] => 20131231
[DTSTART] => 20131226
)
)

[2] => Array
(
[DTEND] => 20121123
[DTSTART] => 20121117
)
)

我已经搜索了很多选项,但我需要能够只选择这两个元素并丢弃其余元素,我将把它与具有不同值的不同数组一起使用,所以不能只指定要删除的元素因为它们会有所不同。

最佳答案

$newArray = array();
foreach ($array as $k => $v) {
$newArray[$k]['DTEND'] = $v['DTEND'];
$newArray[$k]['DTSTART'] = $v['DTSTART'];
}

编辑: 另一种选择,因为您提到尝试使用其中一个数组迭代器函数来做到这一点..

array_walk($array, function(&$v){
$v = array('DTEND' => $v['DTEND'],'DTSTART' => $v['DTSTART']);
});

edit2:因为我没有生命……还有另一种选择……

array_walk($array, function(&$v){
$v = array_intersect_key($v, array_flip(preg_grep('~^DT(END|START)$~', array_keys($v))));
});

我不知道哪个最快/最有效,但我的钱可能在第二个上。

关于php - 从数组中选择特定元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19991821/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com