我正在尝试替换输出数组中的键。缺少值。 缺少的值是'Precio','Monto','Operaciones'和'Hora' - 唯一不会改变的值。
我尝试更改新的密钥名称,但它没有用。
这是代码:
$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);
答案 0 :(得分:1)
$Datos=json_encode($Datos);
$Datos= str_replace($OldKeys,$NewKeys,$Datos);
$Datos=json_decode($Datos, TRUE);

答案 1 :(得分:0)
使用json-encode/decode()
是一种解决方法/ hack,在某些情况下可能会因意外更换错误的键/值而失败。一个更值得信赖的解决方案同样简单。
您已经构建了一个大小等于输入子数组的新键数组。辉煌。 ($OldKeys
和$Claves
不需要。)
$NewKeys=['Accion','Precio','Variacion','CanCompra','PreCompra','PreVenta','CanVenta','PreAnt','PreApe','Max','Min','Vol','Monto','Operaciones','Hora'];
现在您只需要迭代$Datos
数组并将新键应用于每个子数组。
方法#1 array_map()
:
$result=array_map(function($a)use($NewKeys){return array_combine($NewKeys,$a);},$Datos);
或方法#2 foreach()
:
foreach($Datos as $a){
$result[]=array_combine($NewKeys,$a);
}
这是demo link。
如果您致电:var_export($result);
,您会看到:
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',
),
)