如何在此代码中生成我期望的json

时间:2022-01-19 20:20:36

I have one array using i want to make my json response but i am not able do this:

我有一个数组,我想做json响应,但我做不到:

print_r($exist_email);

    Array
(
    [user_id] => 3
)

I tried like this

我试着这样

$response_array['status']='Exist User';
$response_array['message']='Email already exists.';
$response_array['data']=$exist_email;
$this->response($this->json($response_array), 200);

public function response($data,$status=200){
            $this->_code = ($status)?$status:200;
            $this->set_headers();
            echo $data;
            exit;
        }

protected function json($data){
            if(is_array($data)){
                return json_encode($data);
            }
        }

I am getting response

我得到响应

    {
    "status": "Exist User",
    "message": "Email already exists.",
    "data": {
        "user_id": "3"
    }
}

My expected output

我预期的输出

    {
    "status": "Exist User",
    "message": "Email already exists.",
    "data": [
        {
            "user_id": "3"
        }
    ]
}

4 个解决方案

#1


3  

As per my comments you should used multidimensional array in this line $response_array['data']=$exist_email;

根据我的评论,您应该在这行中使用多维数组$response_array['data']=$exist_email;

Above line you can replace with below lines

上面一行可以替换为下面几行

$response_array['data'][0]=$exist_email;

OR

$response_array['data'][]=$exist_email;

Here you can check your desired Output

在这里您可以检查您想要的输出。

#2


1  

If you are expecting an array, you can:

如果您正在等待一个数组,您可以:

$response_array['data'] = array();

$response_array['data'][] = $exist_email;
$response_array['data'][] = $exist_email2; /* For the second, (optional) */

#3


0  

You need to take array to get desired output.

您需要使用数组来获得所需的输出。

You may try below code :

你可以试试下面的代码:

<?php

$response_array['status']='Exist User';
$response_array['message']='Email already exists.';
$response_array['data'][]= array('user_id' => 3);

echo json_encode($response_array, JSON_PRETTY_PRINT);

check : https://3v4l.org/o2QGX

检查:https://3v4l.org/o2QGX

#4


0  

Your expected output is array of array so you need to have $response_array['data'] to be an array.

您的预期输出是数组数组,因此需要将$response_array['data']作为数组。

Just replace this line

只是更换这条线

$response_array['data']=$exist_email;

with

$response_array['data'][]=$exist_email;

#1


3  

As per my comments you should used multidimensional array in this line $response_array['data']=$exist_email;

根据我的评论,您应该在这行中使用多维数组$response_array['data']=$exist_email;

Above line you can replace with below lines

上面一行可以替换为下面几行

$response_array['data'][0]=$exist_email;

OR

$response_array['data'][]=$exist_email;

Here you can check your desired Output

在这里您可以检查您想要的输出。

#2


1  

If you are expecting an array, you can:

如果您正在等待一个数组,您可以:

$response_array['data'] = array();

$response_array['data'][] = $exist_email;
$response_array['data'][] = $exist_email2; /* For the second, (optional) */

#3


0  

You need to take array to get desired output.

您需要使用数组来获得所需的输出。

You may try below code :

你可以试试下面的代码:

<?php

$response_array['status']='Exist User';
$response_array['message']='Email already exists.';
$response_array['data'][]= array('user_id' => 3);

echo json_encode($response_array, JSON_PRETTY_PRINT);

check : https://3v4l.org/o2QGX

检查:https://3v4l.org/o2QGX

#4


0  

Your expected output is array of array so you need to have $response_array['data'] to be an array.

您的预期输出是数组数组,因此需要将$response_array['data']作为数组。

Just replace this line

只是更换这条线

$response_array['data']=$exist_email;

with

$response_array['data'][]=$exist_email;