问题描述
我有两个for循环,都创建了一个关联数组。我需要将第二个数组添加到第一个数组。这是我第一个数组的简单版本
'location' => 'some location'
'color' => 'some color'
'data' =>
array (size=3)
'id' => 1
'name' => 'Name'
'date' => 'some date'
'qty' => null
$data = [];
$addedData = array();
foreach ($dataColors as $dataColor) {
foreach ($existingData as $ed) {
if ($ed['location_id'] === $location && $ed['color_id'] === $dataColor && !in_array($ed['species_id'],$addedData)) {
array_push($addedData,$ed['species_id']);
$data[] = array(
'location' => $ed['location_id'],'color' => $ed['color_id'],'data' => $app['data_manager']->findOne(array('id' => $ed['species_id'])),'qty' => NULL
);
}
}
}
现在,我需要为每个创建的数组将第二个数组添加到qty
键中。这是我尝试过的:
foreach ($data as $key => $value) {
$newData = [];
foreach ($dataFromDatabase as $dfd) {
if ($value['location'] === $dfd['location_id'] && $value['color'] === $dfd['color_id'] && $value['data']['id'] === $dfd['id']) {
$newData[] = array(
'week' => (int)date('W',strtotime($dfd['date'])),'qty' => (int)$dfd['qty']
);
}
}
$value[$key]['qty'] = $newData;
var_dump($value); // this schows the that the data is added to the array
}
var_dump($data); // this doesn't show the added data
怎么可能在for循环之后没有添加添加到数组的数据,而是在for循环内部添加了数据?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)