2011-12-23 9 views
14

Cerco di rimuovere un prefisso nelle chiavi dell'array e ogni tentativo fallisce. Quello che voglio raggiungere è quello di:Come rimuovere il prefisso nelle chiavi dell'array

Avere: Array ([attr_Size] => 3 [attr_Colour] => 7)

di ottenere: Array ([Size] => 3 [Colour] => 7)

Il vostro aiuto sarà molto apprezzato ...

+6

Così si desidera solo per rimuovere 'attr_' dalle vostre chiavi degli array? Cosa c'entra questo con 'implode()'? Dovrebbe 'attr_my_prop' diventare' my_prop', 'prop' o qualcos'altro? Soprattutto, perché? Possiamo vedere il tuo codice "fallito" per favore? –

risposta

3

Uno dei modi per ottenere: Array ([Size] => 3 [Colour] => 7) Dal tuo Having: Array ([attr_Size] => 3 [attr_Colour] => 7)

$new_arr = array(); 
foreach($Your_arr as $key => $value) { 

list($dummy, $newkey) = explode('_', $key); 
$new_arr[$newkey] = $value; 

} 

Se si pensa che ci saranno più di sottolineatura in chiavi basta sostituire prima linea i nside foreach with list($dummy, $newkey) = explode('attr_', $key);

+0

Funziona davvero. Molte grazie. – user1113177

+2

Vedi sotto per una risposta più preferibile. –

+0

Questa non dovrebbe essere la risposta approvata IMO. Vedi sotto per usare la funzione array_keys() più appropriata. Evita il loop in questa risposta. Scusa -1 voto negativo da parte mia. – Ligemer

4

Se ho capito la tua domanda, non devi usare implode() per ottenere quello che vuoi.

define(PREFIX, 'attr_'); 

$array = array('attr_Size' => 3, 'attr_Colour' => 7); 

$prefixLength = strlen(PREFIX); 

foreach($array as $key => $value) 
{ 
    if (substr($key, 0, $prefixLength) === PREFIX) 
    { 
    $newKey = substr($key, $prefixLength); 
    $array[$newKey] = $value; 
    unset($array[$key]); 
    } 
} 

print_r($array); // shows: Array ([Size] => 3 [Colour] => 7) 
+0

Brillante! Lavori. Ora sembra così semplice. Molte grazie. – user1113177

+1

Prego. Per favore non dimenticare di aumentare/accettare le risposte che ti sono state soddisfatte :) –

0

Ecco un'altra cosa da masticare che può essere riutilizzata per più array nell'applicazione che hanno differenti prefissi chiave. Ciò sarebbe utile se si hanno i tasti con prefisso Redis da rimappare o qualcosa del genere.

$inputArray = array('attr_test' => 'test', 'attr_two' => 'two'); 

/** 
* Used to remap keys of an array by removing the prefix passed in 
* 
* Example: 
* $inputArray = array('app_test' => 'test', 'app_two' => 'two'); 
* $keys = array_keys($inputArray); 
* array_walk($keys, 'removePrefix', 'app_'); 
* $remappedArray = array_combine($keys, $inputArray); 
* 
* @param $value - key value to replace, should be from array_keys 
* @param $omit - unused, needed for prefix call 
* @param $prefix - prefix to string replace in keys 
*/ 
function removePrefix(&$value, $omit, $prefix) { 
    $value = str_replace($prefix, '', $value); 
} 

// first get all the keys to remap 
$keys = array_keys($inputArray); 

// perform internal iteration with prefix passed into walk function for dynamic replace of key 
array_walk($keys, 'removePrefix', 'attr_'); 

// combine the rewritten keys and overwrite the originals 
$remappedArray = array_combine($keys, $inputArray); 

// see full output of comparison 
var_dump($inputArray); 
var_dump($remappedArray); 

uscita:

array(2) { 
    'attr_test' => 
    string(4) "test" 
    'attr_two' => 
    string(3) "two" 
} 
array(2) { 
    'test' => 
    string(4) "test" 
    'two' => 
    string(3) "two" 
} 
Problemi correlati