Missing values ​​when replacing keys in a multidimensional array

I am trying to replace keys in my output array. Missing values. Invalid values: "Precio", "Monto", "Operaciones" and "Hora" are the only values ​​that do not change.

I tried to change the new key name, but it didn't work.

This is the code:

$Datos = array(
    0 => array(
        'Simbolo' => 'AA',
        'Precio' => '$ 49,10',
        'Var. %' => '3.15 %',
        'CC.' => '308',
        'PC.' => '49,00',
        'PV.' => '49,10',
        'CV.' => '455',
        'Precio Ant.' => '$ 47,60',
        'Precio Ape.' => '$ 47,60',
        'Max.' => '$ 49,70',
        'Min.' => '$ 47,60',
        'Vol.' => '107.975',
        'Monto' => '5286485',
        'Operaciones' => '214',
        'Hora' => '16:58:21'
    ),
    1 => array(
        'Simbolo' => 'BB',
        'Precio' => '$ 10,35',
        'Var. %' => '0.98 %',
        'CC.' => '41.034',
        'PC.' => '10,30',
        'PV.' => '10,35',
        'CV.' => '7.993',
        'Precio Ant.' => '$ 10,25',
        'Precio Ape.' => '$ 10,40',
        'Max.' => '$ 10,60',
        'Min.' => '$ 10,25',
        'Vol.' => '607.713',
        'Monto' => '6295575',
        'Operaciones' => '245',
        'Hora' => '16:57:57'
    ),
    2 => array(
        'Simbolo' => 'CC',
        'Precio' => '$ 72,80',
        'Var. %' => '4.52 %',
        'CC.' => '600',
        'PC.' => '72,50',
        'PV.' => '72,80',
        'CV.' => '5.900',
        'Precio Ant.' => '$ 69,65',
        'Precio Ape.' => '$ 72,50',
        'Max.' => '$ 72,90',
        'Min.' => '$ 71,05',
        'Vol.' => '1.205.247',
        'Monto' => '86886544',
        'Operaciones' => '1.246',
        'Hora' => '16:58:44'
    )
);

$OldKeys=array('Simbolo','Precio','Var. %','CC.','PC.','PV.','CV.','Precio Ant.','Precio Ape.','Max.','Min.','Vol.','Monto','Operaciones','Hora');

$NewKeys=array('Accion','Precio','Variacion','CanCompra','PreCompra','PreVenta','CanVenta','PreAnt','PreApe','Max','Min','Vol','Monto','Operaciones','Hora');

$Claves=Array(
    'Simbolo' => 'Accion', 
    'Precio' => 'Precio', 
    'Var. %' => 'Variacion',
    'CC.' => 'CanCompra', 
    'PC.' => 'PreCompra', 
    'PV.' => 'PreVenta',
    'CV.' => 'CanVenta', 
    'Precio Ant.' => 'PreAnt',
    'Precio Ape.' => 'PreApe', 
    'Max.' => 'Max', 
    'Min.' => 'Min', 
    'Vol.' => 'Vol', 
    'Monto' => 'Monto',
    'Operaciones' => 'Operaciones', 
    'Hora' => 'Hora' );




function multi_rename_key(&$array, $old_keys, $new_keys)
{
    if(!is_array($array)){
        ($array=="") ? $array=array() : false;
        return $array;
    }
    foreach($array as &$arr){
        if (is_array($old_keys))
        {
            foreach($new_keys as $k => $new_key)
            {
                (isset($old_keys[$k])) ? true : $old_keys[$k]=NULL;
                $arr[$new_key] = (isset($arr[$old_keys[$k]]) ? $arr[$old_keys[$k]] : null);
                unset($arr[$old_keys[$k]]);
            }
        }else{
            $arr[$new_keys] = (isset($arr[$old_keys]) ? $arr[$old_keys] : null);
            unset($arr[$old_keys]);
        }
    }
    return $array;
}

$Datos=multi_rename_key($Datos, $OldKeys, $NewKeys);
print_r($Datos);

      

+3


source to share


2 answers




$Datos=json_encode($Datos);
$Datos= str_replace($OldKeys,$NewKeys,$Datos);
$Datos=json_decode($Datos, TRUE);
      

Run codeHide result


+1


source


Usage json-encode/decode()

is a work around / hack that can fail in some cases by accidentally replacing the wrong keys / values. A more reliable solution is just as easy.


You have already created an array of new keys with a size equal to your input subarrays. Brilliant. ( $OldKeys

and $Claves

not needed.)

$NewKeys=['Accion','Precio','Variacion','CanCompra','PreCompra','PreVenta','CanVenta','PreAnt','PreApe','Max','Min','Vol','Monto','Operaciones','Hora'];

      

Now you just need to iterate through the array $Datos

and apply new keys to each subarray.

Method # 1 array_map()

:



$result=array_map(function($a)use($NewKeys){return array_combine($NewKeys,$a);},$Datos);

      

or method # 2 foreach()

:

foreach($Datos as $a){
    $result[]=array_combine($NewKeys,$a);
}

      

Here is a demo link .

If you call:, var_export($result);

you will see:

array (
  0 => 
  array (
    'Accion' => 'AA',
    'Precio' => '$ 49,10',
    'Variacion' => '3.15 %',
    'CanCompra' => '308',
    'PreCompra' => '49,00',
    'PreVenta' => '49,10',
    'CanVenta' => '455',
    'PreAnt' => '$ 47,60',
    'PreApe' => '$ 47,60',
    'Max' => '$ 49,70',
    'Min' => '$ 47,60',
    'Vol' => '107.975',
    'Monto' => '5286485',
    'Operaciones' => '214',
    'Hora' => '16:58:21',
  ),
  1 => 
  array (
    'Accion' => 'BB',
    'Precio' => '$ 10,35',
    'Variacion' => '0.98 %',
    'CanCompra' => '41.034',
    'PreCompra' => '10,30',
    'PreVenta' => '10,35',
    'CanVenta' => '7.993',
    'PreAnt' => '$ 10,25',
    'PreApe' => '$ 10,40',
    'Max' => '$ 10,60',
    'Min' => '$ 10,25',
    'Vol' => '607.713',
    'Monto' => '6295575',
    'Operaciones' => '245',
    'Hora' => '16:57:57',
  ),
  2 => 
  array (
    'Accion' => 'CC',
    'Precio' => '$ 72,80',
    'Variacion' => '4.52 %',
    'CanCompra' => '600',
    'PreCompra' => '72,50',
    'PreVenta' => '72,80',
    'CanVenta' => '5.900',
    'PreAnt' => '$ 69,65',
    'PreApe' => '$ 72,50',
    'Max' => '$ 72,90',
    'Min' => '$ 71,05',
    'Vol' => '1.205.247',
    'Monto' => '86886544',
    'Operaciones' => '1.246',
    'Hora' => '16:58:44',
  ),
)

      

0


source







All Articles