Aquí hay algo más para masticar que pueden reutilizarse para múltiples matrices en la aplicación que tienen diferentes prefijos clave. Esto sería útil si tiene claves prefijadas de Redis para reasignar o algo de esa naturaleza.
$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);
Salida:
array(2) {
'attr_test' =>
string(4) "test"
'attr_two' =>
string(3) "two"
}
array(2) {
'test' =>
string(4) "test"
'two' =>
string(3) "two"
}
Por lo que sólo desea eliminar 'attr_' de sus claves de matriz? ¿Qué tiene esto que ver con 'implode()'? ¿Debería 'attr_my_prop' convertirse en' my_prop', 'prop' o algo más? Lo más importante, ¿por qué? ¿Podemos ver tu código de "falla" por favor? –