CHarris
CHarris

Reputation: 2803

How do I return my JSON Array in the correct format?

When I echo the variable $contact_username I can see the response in my Android logcat in the form (5 values, which is the correct amount): +11+22+33+44+55.

I'm having trouble returning this as a json array so I can see it in the form,

[{"contact_phonenumber":"+11"},{"contact_phonenumber":"+22"},{"contact_phonenumber":"+33"},{"contact_phonenumber":"+44"},{"contact_phonenumber":"+55"}]

My Php file to echo $contact_username as above is like :

//stuff here
    foreach ($array as $value)
    {
    // stuff here
     $result = $stmt->get_result(); 

     $contact_username = "";

     while ($row = $result->fetch_assoc()) {

     $contact_username = $row['username'];

    }

echo $contact_username;

So echoing $contact_username; gives me +11+22+33+44+55 which I want to return as a JSON Array.

The closest I can get is with the code below but it gives me :

[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][{"contact_phonenumber":"+11"}][][][][][][][][][{"contact_phonenumber":"+22"}][][][] etc... etc...

How can I get it as a JSON Array, and without the empty brackets? Here is my attempt but it's obviously not correct:

//stuff here
    foreach ($array as $value)
    {
    // stuff here
     $result = $stmt->get_result(); 

     $results = [];
     $contact_username = "";

     while ($row = $result->fetch_assoc()) {

     $contact_username = $row['username'];

    array_push($results,['contact_phonenumber' => $contact_username] );

    }

    $json2 = json_encode($results); 
           echo $json2;

EDIT : I'm posting the entire code of my PHP file below

<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
//***************************************************
require('dbConnect.php');
//this is the user_id in the user table
$Number = $_POST['phonenumberofuser'];

// get the username of the user in the user table, then get the matching user_id in the user table
                // so we can check contacts against it 
                $query = "SELECT * FROM user WHERE username = ?";
                $stmt = $con->prepare($query) or die(mysqli_error($con));
                $stmt->bind_param('s', $Number) or die ("MySQLi-stmt binding failed ".$stmt->error);
                $stmt->execute() or die ("MySQLi-stmt execute failed ".$stmt->error);
                $result = $stmt->get_result();

            while ($row = $result->fetch_assoc()) {
            //this is the user_id in the user table of the user
            $user_id = $row["user_id"];
            }

//post all contacts in my phone as a JSON array
$json = $_POST['phonenumberofcontact'];
//decode the JSON
$array = json_decode($json);
//We want to check if contacts in my phone are also users of the app. 
//if they are, then we want to put those phone contacts into the contacts table, as friends of user_id , the user of the app
 $query = "SELECT * FROM user WHERE username = ?";
 $stmt = $con->prepare($query) or die(mysqli_error($con));
 $stmt->bind_param('s', $phonenumberofcontact) or die ("MySQLi-stmt binding failed ".$stmt->error);

 $contacts = [];

 //for each value of phone_number posted from Android, call it $phonenumberofcontact
    foreach ($array as $value)
    {
        $phonenumberofcontact = $value->phone_number;

$stmt->execute() or die ("MySQLi-stmt execute failed ".$stmt->error);
//store the result of contacts from the user's phonebook (that is, the result of the above query, $stmt) that are using the app
     $result = $stmt->get_result(); 

     //In this while loop, check the $phonenumberofcontact in the user's phonebook and who are users of the app against
     //the user's contacts table. Put the shared contacts in the contacts table for that user.
            while ($row = $result->fetch_assoc()) {

            $contacts[]["contact_phonenumber"] = $row['username'];  
    }

     echo json_encode($contacts);

    }

$stmt->close();

        ?>

Upvotes: 0

Views: 50

Answers (2)

CHarris
CHarris

Reputation: 2803

Instead of :

$contacts = [];
    foreach ($array as $value)
    {
        $result = $stmt->get_result(); 

        while ($row = $result->fetch_assoc()) {
            $contacts[]["contact_phonenumber"] = $row['username'];
        }
    }

It should be :

 $results = array();
    foreach ($array as $value)
    {
        $result = $stmt->get_result(); 

                if(!empty($row['username'])) {
                $results[] = array('contact_phonenumber' => $row['username']);
                        }
    }

Upvotes: 0

Anis Alibegić
Anis Alibegić

Reputation: 3230

$contacts = [];

foreach ($array as $value)
{
    $result = $stmt->get_result(); 

    while ($row = $result->fetch_assoc()) {
        $contacts[]["contact_phonenumber"] = $row['username'];
    }
}

echo json_encode($contacts);

...will produce: [{"contact_phonenumber":"+11"},{"contact_phonenumber":"+22"},{"contact_phonenumber":"+33"},{"contact_phonenumber":"+44"},{"contact_phonenumber":"+55"}].

Upvotes: 1

Related Questions