阵列填充缺失索引 [英] Array populate missing index
本文介绍了阵列填充缺失索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有一个code,它显示数组里面同样有EID天的数组
Have a code that display array with same eid inside it have an array of days
foreach ($data['display'] as $item) {
$eid = implode($data['data'][$i]['eid']);
$date = date('Y-m',strtotime($item['date_d'][0]));
if (!isset($groups[$eid])) {
$groups[$eid] = array(
'employeeId' => $eid,
'efullname' => $item['fullname'][0],
'hrsdiff' => $item['hrsdiff'][0],
'days' => array(
$date => $item['key'][0],
)
);
} else {
@$groups[$eid]['days'][$date] += $item['key'][0];
}
$i++;
}
这显示:
[0] => Array
(
[employeeId] => 21
[efullname] => MANOGURA EDGAR
[hrsdiff] => 2.2331
[days] => Array
(
[2014-02] => 23
[2014-03] => 26
[2014-04] => 23
)
)
[1] => Array
(
[employeeId] => 1
[efullname] => IBARDOLAZA VIRGILIO
[hrsdiff] => 5.0333
[days] => Array
(
[2014-01] => 2
[2014-02] => 24
[2014-03] => 26
[2014-04] => 26
)
)
希望有天数组,如果缺少它会添加缺少的指数天和值必须在索引0前有相同的索引[0]天数组应具有[2014-01] => 0
want to have the days array to have same index if missing it will add the missing index days and the value must be 0 ex in index[0] the days array should have [2014-01] => 0
推荐答案
假设两个数组其中丢失的钥匙可以在其中任何一个:
Assuming two arrays of which the missing keys could be in either of them:
$a = array('2014-02' => 'afoo', '2014-03' => 'abar', '2014-04' => 'abaz');
$b = array('2014-02' => 'bfoo', '2014-03' => 'bbar', '2014-01' => 'bbaz');
$a += array_fill_keys(array_keys(array_diff_key($b, $a)), 0);
$b += array_fill_keys(array_keys(array_diff_key($a, $b)), 0);
print_r($a);
print_r($b);
输出:
Array
(
[2014-02] => afoo
[2014-03] => abar
[2014-04] => abaz
[2014-01] => 0
)
Array
(
[2014-02] => bfoo
[2014-03] => bbar
[2014-01] => bbaz
[2014-04] => 0
)
这篇关于阵列填充缺失索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文