2010-02-10 98 views
1

我有這個巨大的數組:如何用鍵等於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" => 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":""}" 
) 

我想刪除它的鍵啓動與下列任何的元素: 「specifications_」, 「amenities_」, 「facilities_」, 「consumables_」, 「financials_」

我想過使用array_filter與回調,但我不知道如何做回調中的事情。請幫忙。

回答

5

您實際上不能使用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; 
    } 
} 

現在這個只要前綴列表是沒有那麼大,陣列是沒有那麼大,但也沒有必要,除非你需要它過於複雜的解決方案效果相當好。

2

array_filter()只傳遞值,因此不適合此任務。與foreach()迭代遍歷數組,將要保留的元素添加到新數組中。

+0

@Felix:當然可以,但是,這並不意味着你可以* *測試它們。 – 2010-02-10 08:41:40

+0

對不起,現在我明白你的意思了。 – 2010-02-10 09:33:16

1
<?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; 
     } 
    } 
0

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); 

請注意,我迭代你的數組的鍵。這是因爲不允許更改用於array_walk的陣列(或者至少會導致不可預知的行爲)。

如果你想保持原有的陣列完好,你必須先複製:

$array_copy = $array;  
array_walk(array_keys($array), 'filter', &$array_copy); 
相關問題