2010-02-10 9 views
1

J'ai ce grand tableau:Comment supprimer des éléments de tableau avec des clés égales à un certain mot en 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":""}" 
) 

Je veux supprimer les éléments dont les clés commencer par une des opérations suivantes: "specifications_", "amenities_", "facilities_", "consumables_", "financials_"

J'ai pensé utiliser array_filter avec un callback mais je n'ai aucune idée de comment faire les choses dans le callback. S'il vous plaît aider.

Répondre

5

Vous ne pouvez pas réellement utiliser array_filter() pour cela car il préserve les clés mais vous ne pouvez pas filtrer sur les touches, ce qui est votre problème. Dans ce cas, vous pouvez aussi simplement faire une boucle.

$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; 
    } 
} 

Maintenant, cela fonctionne assez bien tant que la liste des préfixes est pas grande et le tableau est pas grande, mais il n'y a pas besoin de la solution à moins overcomplicate vous en avez besoin. Ne fait que passer la valeur et est donc inadapté à cette tâche.

2

array_filter() Itérer à travers le tableau avec foreach(), en ajoutant les éléments que vous souhaitez conserver à un nouveau tableau.

+0

@Felix: Bien sûr, mais cela ne signifie pas que vous pouvez * Test * eux. –

+0

Ah désolé, maintenant j'ai compris ce que vous voulez dire. –

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

Qu'en est-il de 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); 

Notez que je répète sur les clés de votre tableau. C'est parce qu'il n'est pas permis de changer le tableau qui est utilisé pour array_walk (ou du moins cela conduit à un comportement imprévisible).

Si vous voulez garder le tableau original intact, vous devez copier d'abord:

$array_copy = $array;  
array_walk(array_keys($array), 'filter', &$array_copy); 
Questions connexes