-2

Given the following JSON

$first = array('code'=>'200','message'=>'ok');
{
"code": "200",
"message": "ok"
}


$second = array("user"=>array('fname'=>'Fred','lname'=>'Flintstone','status'=>'1'))
{
"user": [
    {
        "fname": "Fred",
        "lname": "Flintstone",
        "status": "1"
    }
]
}

How do I combine these to get the output as follows.

{
"code": "200",
"message": "ok",
"user": [
    {
        "fname": "Fred",
        "lname": "Flintstone",
        "status": "1"
    }
]
}
4

3 回答 3

2

尝试合并数组

$json = json_encode(array_merge($first, $second));
于 2013-11-06T22:38:22.093 回答
0

也许:

$first = array('code' => '200', 'message' => 'ok');
$second = array('user' => array('fname' => 'Fred', 'lname' => 'Flintstone', 'status' => '1'));

$array = $first + $second; 
$json = json_encode($array);
于 2013-11-06T22:38:42.517 回答
0

使用array_merge您可以组合数组然后对其进行编码:

//arrays
$first = array('code'=>'200','message'=>'ok');
$second = array("user"=>array('fname'=>'Fred','lname'=>'Flintstone','status'=>'1'));

//merging
$merged_arrays = array_merge($first, $second);
print_r($merged_arrays);

//encoding
$json_data = json_encode($merged_arrays);
echo $json_data;
于 2013-11-06T22:45:45.193 回答