0

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();

        ?>
4
  • The empty brackets are not due to JSON encoding, your loop is causing them. Perhaps you should instead use the more common method of appending an array to simplify your code. $results[] = ['name' => 'value'] Commented Aug 5, 2017 at 0:03
  • json_encode couldn't possibly generate output like that. The //stuff here might have been relevant. Commented Aug 5, 2017 at 0:04
  • Do you mind showing us the result you get by typing print_r($array);? Commented Aug 5, 2017 at 0:40
  • When I type print_r($array) in my Android logcat I see Array ( [0] => stdClass Object ([phone_number] => +1234567) [1] => stdClass Object ([phone_number] => +3456789) [2] => stdClass Object ([phone_number] => +6789098) [3] => stdClass Object ([phone_number] => +7654321) etc Commented Aug 5, 2017 at 0:53

2 Answers 2

1
$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"}].

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks but what I get now is: [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][{"contact_phonenumber":"+11"}][{"contact_phonenumber":"+11"}][{"contact_phonenumber":"+11"}][{"contact_phonenumber":"+11"}][{"contact_phonenumber":"+11"}][{"contact_phonenumber":"+11"}][{"contact_phonenumber":"+11"}][{"contact_phonenumber":"+11"}][{"contact_phonenumber":"+11"}][{"contact_phonenumber":"+11"},{"contact_phonenumber":"+22"}][{"contact_phonenumber":"+11"},{"contact_phonenumber":"+22"}] etc etc...I'll post all my code in the question if that's any help.
Then your loops are not working correctly. I tried on simple values and it worked. Let us see the rest of the code, for example $array.
0

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']);
                        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.