1

I have to combine two array into single inside Json encode. my code is,

    $email  =   $_GET["email"];
    $password   =   $_GET["password"];

    $query      =   "SELECT * FROM tbl_user_login WHERE email='$email' AND password='$password' AND verification='1'";
    $result     =   mysqli_query($c, $query) or die(mysqli_error($c));
    $length     =   mysqli_num_rows($result);

    if($length == 1)
    {
        $var[]  =   array('status'=>"success");

        while($obj = mysqli_fetch_object($result)) 
        {
            $var[] = $obj;
        }
        echo '{"login":'.json_encode($var).'}';
    }
    else
    {
        $arr    =   array('status'=>"notfound");
        echo '{"login":['.json_encode($arr).']}';
    }

Now the result is,

{"login":[{"status":"success"},{"login_id":"1","name":"Jithin Varghese","password":"some","phone":"","email":"[email protected]","addr":"","city":"","state":"","pincode":"0","type":"STD","verification":"1"}]}

And the require output is,

{"login":[{"status":"success","login_id":"1","name":"Jithin Varghese","password":"some","phone":"","email":"[email protected]","addr":"","city":"","state":"","pincode":"0","type":"STD","verification":"1"}]}

How to combine array. I have tried a lot.

1
  • You must not use User Input in your SQL Query without validation, you're vunerable to an SQL Injection Attack. Please have a look at prepared statements Commented Dec 16, 2015 at 11:42

3 Answers 3

4

Change

$var[] = array('status'=>"success");
while($obj = mysqli_fetch_object($result)) 
{
    $var[] = $obj;
}

to

$var['status'] = "success";
// use the assoc fetch here.. to avoid casting to array
while($arr = mysqli_fetch_assoc($result)) 
{
    $var = array_merge($var, $arr);
}
Sign up to request clarification or add additional context in comments.

1 Comment

now the result is {"login":{"status":"success","0":{"login_id":"1","name":"Jithin Varghese","password":"some","phone":"","email":"[email protected]","addr":"","city":"","state":"","pincode":"0","type":"STD","verification":"1"}}}
1

You can use array_merge to get the exact output you request:

$email  =   $_GET["email"];
$password   =   $_GET["password"];

$query      =   "SELECT * FROM tbl_user_login WHERE email='$email' AND password='$password' AND verification='1'";
$result     =   mysqli_query($c, $query) or die(mysqli_error($c));
$length     =   mysqli_num_rows($result);

$response = [];
if($length == 1)
{
    $response['login'] = arry(array_merge(array('status'=>"success"), mysqli_fetch_assoc($result)));
}
else
{
    $response['login'] = array(array('status'=>"notfound"));
}

header('Content-Type: application/json');
echo json_encode($response);

Note that it seems unnecessary to have login property be an array when there is only one result, so it would make sense to remove the outer array wrap:

if($length == 1)
{
    $response['login'] = array_merge(array('status'=>"success"), mysqli_fetch_assoc($result));
}
else
{
    $response['login'] = array('status'=>"notfound");
}

Comments

0

It's a wild guess, but try this.

$result = array(
    'login' => $var
);

echo json_encode($result);

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.