在PHP中,如何删除键等于某个单词的数组元素?
我有一个巨大的阵列:在PHP中,如何删除键等于某个单词的数组元素?,php,Php,我有一个巨大的阵列: array(47) ( "name" => string(0) "" "state" => string(7) "Alabama" "city" => string(0) "" "country" => string(13) "United States" "location" => string(0) "" "rooms" => string(0) "" "price" =>
array(47) (
"name" => string(0) ""
"state" => string(7) "Alabama"
"city" => string(0) ""
"country" => string(13) "United States"
"location" => string(0) ""
"rooms" => string(0) ""
"price" => string(0) ""
"highlights" => array(5) (
0 => string(0) ""
1 => string(0) ""
2 => string(0) ""
3 => string(0) ""
4 => string(0) ""
)
"specifications_type" => string(0) ""
"specifications_lotsize" => string(0) ""
"specifications_apn" => string(0) ""
"specifications_interest" => string(0) ""
"specifications_year" => string(0) ""
"specifications_buildings" => string(0) ""
"specifications_stories" => string(0) ""
"specifications_corridors" => string(0) ""
"financials_room" => string(0) ""
"financials_other" => string(0) ""
"financials_total" => string(0) ""
"financials_multiplier" => string(0) ""
"financials_caprate" => string(0) ""
"financials_netincome" => string(0) ""
"amenities_pool" => string(0) ""
"amenities_fitness" => string(0) ""
"amenities_quarters" => string(0) ""
"amenities_services" => string(0) ""
"amenities_spa" => string(0) ""
"amenities_meetspace" => string(0) ""
"amenities_parkspace" => string(0) ""
"amenities_others" => string(0) ""
"facilities_room" => string(0) ""
"facilities_hvac" => string(0) ""
"facilities_furnitures" => string(0) ""
"facilities_tv" => string(0) ""
"facilities_ref" => string(0) ""
"facilities_microwave" => string(0) ""
"consumables_resto" => string(0) ""
"consumables_restocpct" => string(0) ""
"consumables_barlounge" => string(0) ""
"consumables_barloungecpct" => string(0) ""
"consumables_bevrevenue" => string(0) ""
"consumables_others" => string(0) ""
"specifications" => string(100) "{"type":"","lotsize":"","apn":"","interest":"","year":"","buildings":"","stories":"","corridors":""}"
"consumables" => string(89) "{"resto":"","restocpct":"","barlounge":"","barloungecpct":"","bevrevenue":"","others":""}"
"amenities" => string(100) "{"type":"","lotsize":"","apn":"","interest":"","year":"","buildings":"","stories":"","corridors":""}"
"facilities" => string(100) "{"type":"","lotsize":"","apn":"","interest":"","year":"","buildings":"","stories":"","corridors":""}"
"financials" => string(100) "{"type":"","lotsize":"","apn":"","interest":"","year":"","buildings":"","stories":"","corridors":""}"
)
我要删除其键以以下任意一项开头的元素:
“规格”、“便利设施”、“设施”、“消耗品”、“财务”
我曾想过在回调中使用array_filter,但我不知道如何在回调中进行操作。请提供帮助。您实际上不能为此使用
数组过滤器()
,因为它保留键,但您不能对键进行过滤,这是您的问题。在这种情况下,您也可以循环
$prefix = array(
'specifications_',
'amenities_',
'facilities_',
'consumables_',
'financials_',
);
$output = array();
foreach ($input as $k => $v) {
foreach ($prefix as $p) {
$add = true;
if (strpos($k, $p) === 0) {
$add = false;
break;
}
}
if ($add) {
$output[$k] = $v;
}
}
现在,只要前缀列表没有那么大,数组也没有那么大,这种方法就可以很好地工作,但除非您需要,否则无需过度复杂化解决方案。您不能实际使用
array\u filter()
,因为它保留键,但不能对键进行过滤,这是您的问题。在这种情况下,您也可以循环
$prefix = array(
'specifications_',
'amenities_',
'facilities_',
'consumables_',
'financials_',
);
$output = array();
foreach ($input as $k => $v) {
foreach ($prefix as $p) {
$add = true;
if (strpos($k, $p) === 0) {
$add = false;
break;
}
}
if ($add) {
$output[$k] = $v;
}
}
现在,只要前缀列表没有那么大,数组也没有那么大,这个方法就可以很好地工作,但除非您需要,否则无需过度复杂化解决方案。只传递值,因此不适合此任务。使用遍历数组,将要保留的元素添加到新数组。只传递值,因此不适合此任务。使用遍历数组,将要保留的元素添加到新数组中。如何
<?php
$source_array = array([..the yours array..]);
$temp_array = array();
$exclude_keys = array('specifications', 'amenities', 'facilities', 'consumables', 'financials');
foreach ($source_array as $key => $value) {
if (!in_array(substr($key, 0, strpos('_') - 1), $exclude_keys)) {
$temp_array[$key] = $value;
}
}
注意,我迭代了数组的键。这是因为不允许更改用于array\u walk
的数组(或者至少会导致不可预测的行为)
如果要保持原始阵列的完整性,必须先复制它:
$array_copy = $array;
array_walk(array_keys($array), 'filter', &$array_copy);
那怎么办
注意,我迭代了数组的键。这是因为不允许更改用于array\u walk
的数组(或者至少会导致不可预测的行为)
如果要保持原始阵列的完整性,必须先复制它:
$array_copy = $array;
array_walk(array_keys($array), 'filter', &$array_copy);
@菲利克斯:当然,但这并不意味着你可以测试它们。啊,对不起,现在我明白你的意思了。@菲利克斯:当然,但这并不意味着你可以测试它们。啊,对不起,现在我明白你的意思了。