Reputation: 12957
I'm having two arrays i'm inserting one array into another. This operation is done for each element of first array using foreach loop. But I'm able to attach only first pair of key value pairs to the first array during second iteration it does not. Can you help me in achieving this for aal array elements? My code is as follows:
$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'];
}
The first array named $data is as follows:
Array
(
[0] => Array
(
[user_id] => 9def02e6337b888d6dbe5617a172c18d
[user_first_name] => Ashutosh
[user_last_name] => Modi
[user_email] => [email protected]
[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] => [email protected]
[user_status] => enable
[user_subscription] => period
[user_registered_type] => online
[user_reg_date] => 1325152066
)
)
The first iteration of second array named $user_data
is as follows:
Array
(
[user_state] => Rajasthan
[user_city] => Jhunjhunu
)
Now after attaching this the array $data becomes as follows:
Array
(
[0] => Array
(
[user_id] => 9def02e6337b888d6dbe5617a172c18d
[user_first_name] => Ashutosh
[user_last_name] => Modi
[user_email] => [email protected]
[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] => [email protected]
[user_status] => enable
[user_subscription] => period
[user_registered_type] => online
[user_reg_date] => 1325152066
[user_state] =>
[user_city] =>
)
)
I'm not getting why the second array is having the blank values for user_state and user-city. Even after having the values of the array $user_data
in second iterations are as follows:
Array
(
[user_state] => Arunachal Pradesh
[user_city] => Shollong
)
Can you resolve my this issue. Thanks in advance.
Upvotes: 1
Views: 104
Reputation: 24
Your insertion is lie inside array of array. Try this
$i=0;
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[$i]['user_state'] = $user_data['user_state'];
$data[$i]['user_city'] = $user_data['user_city'];
$i++;
where $i represent the array key inside your data file which is default indexing start from 0 & go to 1,2 & so on in your data file..
Also Explore the following program may be find any thing good & being helpful in form to sort out your problem. e.g a program to update the array inside array.
<?php
$d3 = array('0'=>array('b'=>'1','c'=>'2'),'1'=>array('b'=>'3','c'=>'4'));
$i=0;
foreach($d3 as $k => $v){
$d3[$i]['b'] = "h";
$i++;
}
print_r($d3);
?>
Upvotes: 0
Reputation: 550
user_state and user_city are both added, so your code is working fine. You gotta know why $user_data is empty there.
Make your code like this:
echo $sql;
print_r($user_data);
$data[$key]['user_state'] = $user_data['user_state'];
$data[$key]['user_city'] = $user_data['user_city'];
That will output your sql for each loop and print_r function (or var_dump) will echo $user_data array right before those lines. Check why it is empty. Probably your second query in that loop is not what you think it is.
Upvotes: 1