我有一个像这样的数组:
array(
array(
'Category' => 'Revisiones Operativas',
'GQ' => '0',
'Comment' => ''
),
array(
'Category' => 'Estatus de las Revisiones Operativas',
'GQ' => '0',
'Comment' => ''
),
array(
'Category' => 'Tendencias OEC',
'GQ' => '0',
'Comment' => ''
),
array(
'Category' => 'Reportes / Returns',
'GQ' => '0',
'Comment' => ''
),
array(
'Category' => 'GLs Diferencias de Cajeros / ABM',
'GQ' => '0',
'Comment' => ''
),
array(
'Category' => 'Cuentas GL Suspenso',
'GQ' => '0',
'Comment' => ''
),
array(
'Category' => 'Exedentes de Efectivo',
'GQ' => '0',
'Comment' => ''
),
array(
'Category' => 'ACT / BF',
'GQ' => '0',
'Comment' => ''
),
array(
'Category' => 'Ranking de Cajeros',
'GQ' => '0',
'Comment' => ''
),
array(
'Category' => 'Sessiones sin PIN (%)',
'GQ' => '0',
'Comment' => ''
),
array('Category' => 'Transacciones Invalidas',
'GQ' => '0',
'Comment' => ''
)
);
我需要在我的数组中的每个数组中插入一个Date
值。有点像这样:
array(
array(
'Category' => 'Revisiones Operativas',
'GQ' => '0',
'Comment' => ''
'Date' => '1990/12/01'
),
etc...
我该怎么办?是否有我已经可以使用的功能,或者我必须迭代数组并为每条记录添加它?
更新
我有这个功能:
public function insertDate($array, $date){
foreach($array as $arr){
$arr['Date'] = date;
}
return $array;
}
我只是想知道php中是否已经存在一个方法来执行此操作
答案 0 :(得分:2)
看看php中的内置数组函数:https://secure.php.net/manual/en/book.array.php。你可以使用的是array_map:
<?php
$array = [
[
'Category' => 'Revisiones Operativas',
'GQ' => '0',
'Comment' => ''
],
[
'Category' => 'Estatus de las Revisiones Operativas',
'GQ' => '0',
'Comment' => ''
]
];
$newArray = array_map(function($each){
return $each + [
'Date' => '1990/12/01'
];
}, $array);
print_r($newArray);
上述脚本将给出以下结果:
Array
(
[0] => Array
(
[Category] => Revisiones Operativas
[GQ] => 0
[Comment] =>
[Date] => 1990/12/01
)
[1] => Array
(
[Category] => Estatus de las Revisiones Operativas
[GQ] => 0
[Comment] =>
[Date] => 1990/12/01
)
)
最好的问候:)
<强>更新强>:
如果您不想创建另一个阵列,也可以使用array_walk。以下脚本将生成与上面相同的输出:
<?php
$array = [
[
'Category' => 'Revisiones Operativas',
'GQ' => '0',
'Comment' => ''
],
[
'Category' => 'Estatus de las Revisiones Operativas',
'GQ' => '0',
'Comment' => ''
]
];
array_walk($array, function(&$each){
$each['Date'] = '1990/12/01';
});
print_r($array);
答案 1 :(得分:1)
您需要遍历每个子数组,并为每个子数组添加date
索引。
<?php
$array = array(
array(
'Category' => 'Revisiones Operativas',
'GQ' => '0',
'Comment' => ''
),
array(
'Category' => 'Estatus de las Revisiones Operativas',
'GQ' => '0',
'Comment' => ''
)
); // This is your array
for ($i = 0; $i < sizeof($array); $i++) {
// Iterate through the array and add the date index
$array[$i]['Date'] = date('Y/m/d');
}
print_r($array); // Print the array to verify the change
调用date
函数以当前日期插入/更新当前日期索引,或者您可以根据需要编写静态日期。