Sid Sid - 1 month ago 9
Android Question

how to add another row in one array?

I am getting the array of invitations and for each invitation's row by sender_id from invitation row I am retrieving user's detail from users table.

I want to add user's detail in the same invitations array. I tried to add like this.

function getInvitations()
{
$database = new Database(ContactsConstants::DBHOST,ContactsConstants::DBUSER,ContactsConstants::DBPASS,ContactsConstants::DBNAME);
$dbConnection = $database->getDB();

$stmt = $dbConnection->prepare("SELECT * FROM `Invitation` WHERE Invitation.invitee_no = ?");
$stmt->execute(array($this-> invitee_no));
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

$invitations = array();

if (count($rows) > 0) {

foreach($rows as $row)
{
$stmt = $dbConnection->prepare("Select * from Users where user_id =?");
$stmt->execute(array($row['sender_id']));

$result = $stmt->fetch(PDO::FETCH_ASSOC);

$invitations[] = $row;
$invitations[] = $result;
}

$response = array("status" => 1, "message" => "Success", "Invitations" => $invitations);
return json_encode($response);
}

else {
$response = array("status"=>-1,"message"=>"Invitations list is empty");
return json_encode($response);
}
}


output I am getting like this:

{
"status": 1,
"message": "Success",
"Invitations": [
{
"invitation_id": "369",
"date": "12/08/2016",
"invitee_no": "8655864341",
"status": "1",
"sender_id": "44"
},
{
"user_id": "44",
"user_name": "Angel",
"password": "angel",
"profile_image": "a8625093a2e95ee6b5042456b7cccc20.png",
"device_id": "eVOnz8AMVOs:APA91bHuq9Q4FLcOewUCSAQu79aZNeQsb4710k66g1MHDfZUZWa4ZcARs0SekUfnYUt5sTiKeXfFk_Gg_LdtSKk-pTAaQLxPe9UMkYG9-d-IkDRlGYxkzonQ1h2YN78S7RU0x90NAby_",
"mobile_no": "9090966666",
"email_id": "kjfhg@half.com",
"full_name": "ajjdjd",
"job_title": "sxsaca",
"home_address": "scacac",
"work_phone": "ugiuygitf",
"work_address": "bbuyiytfi"
},
]}


But I want both details in one array. How can I get this?
Can anyone help with this? Thank you..

EDIT:

I am trying to get the user_name the string from invitations array in android. But its giving no value for user_name exception. Though the value is present in array.

@Override
public void doPostExecute(JSONArray response) throws JSONException {

for (int i = 0; i < response.length(); i++) {

// Invitation invitation = new Invitation();
JSONObject subObject1 = response.getJSONObject(i);

Invitation invitation = new Invitation();

String status = subObject1.getString("status");
String number = subObject1.getString("invitee_no");
String userName = subObject1.getString("user_name");


Exception:

p W/System.err: org.json.JSONException: No value for user_name


Invitations array:

"Invitations": [
{
"invitation_id": "369",
"date": "12/08/2016",
"invitee_no": "8655864341",
"status": "1",
"sender_id": "44",
"user_id": "44",
"user_name": "Angel",
"password": "angel",
"profile_image": "a8625093a2e95ee6b5042456b7cccc20.png",
"device_id": "eVOnz8AMVOs:APA91bHuq9Q4FLcOewUCSAQu79aZNeQsb4710k66g1MHDfZUZWa4ZcARs0SekUfnYUt5sTiKeXfFk_Gg_LdtSKk-pTAaQLxPe9UMkYG9-d-IkDRlGYxkzonQ1h2YN78S7RU0x90NAby_",
"mobile_no": "9090966666",
"email_id": "kjfhg@half.com",
"full_name": "ajjdjd",
"job_title": "sxsaca",
"home_address": "scacac",
"work_phone": "ugiuygitf",
"work_address": "bbuyiytfi"
},


Can anyone help with this?

Answer

The problem is that you are pushing to $invitations variable two different associative arrays. The solution here is to combine the elements of both arrays (array_merge) into only one, and then push it to $invitation.

Try to change this code:

$invitations[] = $row;
$invitations[] = $result;

by this:

$invitations[] = array_merge($row, $result);

Edit:

To solve the warning array_merge(): Argument #2 is not an array try to force $result to array type with a casting:

$invitations[] = array_merge($row, (array)$result);