我正在尝试替换输出数组中的键。缺少值。
缺少的值是“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);
最佳答案
$Datos=json_encode($Datos);
$Datos= str_replace($OldKeys,$NewKeys,$Datos);
$Datos=json_decode($Datos, TRUE);
关于php - 替换多维数组中的键时缺少值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44090170/