3

I'm trying to replace the keys in my output array. There are missing values. The missing values are ‘Precio’, ‘Monto’, ‘Operaciones’ and ‘Hora’ -- the only values that don't change.

I tried changing the new key name but it didn't worked.

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);
2
  • You should try to simplify your code. It is way too complicated. Commented May 20, 2017 at 20:01
  • SOLVED: $Datos=json_encode($Datos); $Datos= str_replace($OldKeys,$NewKeys,$Datos); $Datos=json_decode($Datos, TRUE); Commented May 20, 2017 at 20:58

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

Using json-encode/decode() is a work around / hack that can fail in some cases by accidentally replacing the wrong keys/values. A more trustworthy solution is just as simple.


You have already built an array of new keys with a size equal to your input subarrays. Brilliant. ($OldKeys and $Claves are not needed.)

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

Now you just need to iterate your $Datos array and apply the 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 the 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',
  ),
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.