I have this huge array:
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) "{"开发者_如何学Gotype":"","lotsize":"","apn":"","interest":"","year":"","buildings":"","stories":"","corridors":""}"
"financials" => string(100) "{"type":"","lotsize":"","apn":"","interest":"","year":"","buildings":"","stories":"","corridors":""}"
)
I want to remove the elements whose keys start with any of the following: "specifications_", "amenities_", "facilities_", "consumables_", "financials_"
I have thought of using array_filter with a callback but I have no idea how to do things in the callback. Please help.
You can't actually use array_filter()
for this because it preserves keys but you can't filter on keys, which is your problem. In that case you may as well just loop.
$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;
}
}
Now this works reasonably well so long as the list of prefixes isn't that large and the array isn't that large but there is no need to overcomplicate the solution unless you need it.
array_filter()
only passes the value and so is unsuitable for this task. Iterate through the array with foreach()
, adding the elements you want to keep to a new array.
<?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;
}
}
What about array_walk
?
$prefix = array(
'specifications_',
'amenities_',
'facilities_',
'consumables_',
'financials_',
);
function filter($v, $k, &$data) {
global $prefix;
foreach($prefix as $p) {
if(strpos($v, $p) === 0) {
unset($data[$v])
break;
}
}
}
array_walk(array_keys($array), 'filter', &$array);
Note that I iterate over the keys of your array. This is because it is not allowed to change the array that is used for array_walk
(or at least it leads to unpredictable behavior).
If you want to keep the original array intact, you have to copy it first:
$array_copy = $array;
array_walk(array_keys($array), 'filter', &$array_copy);
精彩评论