我有两个数组,我将一个数组插入另一个数组。使用 foreach 循环对第一个数组的每个元素完成此操作。但是我只能在第二次迭代期间将第一对键值对附加到第一个数组,但它不能。你能帮我为 aal 数组元素实现这个吗?我的代码如下:
$data = $grid->GetData();
//print_d($data);
foreach($data as $key => $value) {
$sql =" SELECT users_details.user_state, users_details.user_city FROM ".TBL_USERS." AS user JOIN ";
$sql .= TBL_USERS_DETAILS." AS users_details on user.user_id = users_details.user_id ";
$sql .=" WHERE user.user_id = '". $value['user_id']."'" ;
$gDb->Query( $sql );
$user_data = $gDb->FetchArray(MYSQL_FETCH_SINGLE);
$data[$key]['user_state'] = $user_data['user_state'];
$data[$key]['user_city'] = $user_data['user_city'];
}
第一个名为 $data 的数组如下:
Array
(
[0] => Array
(
[user_id] => 9def02e6337b888d6dbe5617a172c18d
[user_first_name] => Ashutosh
[user_last_name] => Modi
[user_email] => ashutosh.modi@gmail.com
[user_status] => enable
[user_subscription] => lifetime
[user_registered_type] => online
[user_reg_date] => 1325581397
)
[1] => Array
(
[user_id] => a6d22e4cc3f65778a60b359842bcec82
[user_first_name] => Dilip
[user_last_name] => Modi
[user_email] => dm.modi@gmail.com
[user_status] => enable
[user_subscription] => period
[user_registered_type] => online
[user_reg_date] => 1325152066
)
)
命名的第二个数组的第一次迭代$user_data
如下:
Array
(
[user_state] => Rajasthan
[user_city] => Jhunjhunu
)
现在附加这个后,数组 $data 变成如下:
Array
(
[0] => Array
(
[user_id] => 9def02e6337b888d6dbe5617a172c18d
[user_first_name] => Ashutosh
[user_last_name] => Modi
[user_email] => ashutosh.modi@gmail.com
[user_status] => enable
[user_subscription] => lifetime
[user_registered_type] => online
[user_reg_date] => 1325581397
[user_state] => Rajasthan
[user_city] => Jhunjhunu
)
[1] => Array
(
[user_id] => a6d22e4cc3f65778a60b359842bcec82
[user_first_name] => Dilip
[user_last_name] => Modi
[user_email] => dm.modi@gmail.com
[user_status] => enable
[user_subscription] => period
[user_registered_type] => online
[user_reg_date] => 1325152066
[user_state] =>
[user_city] =>
)
)
我不明白为什么第二个数组具有user_state 和 user-city的空白值。即使$user_data
在第二次迭代中数组的值如下:
Array
(
[user_state] => Arunachal Pradesh
[user_city] => Shollong
)
你能解决我的这个问题吗。提前致谢。