Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将包含对象的数组转换为字符串_Php_Json - Fatal编程技术网

Php 将包含对象的数组转换为字符串

Php 将包含对象的数组转换为字符串,php,json,Php,Json,我在执行$result1=内爆(“”,$result1) 我使用2个查询并希望将结果合并为一个,以下是我的查询: if($stmt->execute()){ $user = $stmt->get_result(); while ($obj = $user->fetch_object()) { $result1[] = $obj; } } if($stmt->execute()){ $user = $stmt->g

我在执行
$result1=内爆(“”,$result1)

我使用2个查询并希望将结果合并为一个,以下是我的查询:

if($stmt->execute()){
    $user = $stmt->get_result();
    while ($obj = $user->fetch_object()) {
         $result1[] = $obj;
    }

}

if($stmt->execute()){
    $user = $stmt->get_result();
    while ($obj = $user->fetch_object()) {
         $result2[] = $obj;
    }

}
  • 在此处将$result1和result2转换为字符串
  • 我需要$result1和result2

    '[{
    “uId”:“1”,
    “名字”:“詹姆斯”,
    “姓氏”:“债券”
    }]“

这样它就可以在代码下面运行

$arr1 = json_decode($result1,true);
$arr2 = json_decode($result2,true);
$arr1[0]['task'] = $arr2;
$finalJSON = json_encode($arr1);
echo $finalJSON;

$result1
$result2
已经是数组了

删除以下行:

$arr1 = json_decode($result1,true);
$arr2 = json_decode($result2,true);
并将
fetch\u对象
更改为
fetch\u数组


或更改
$arr1[0]['task']=$arr2
$arr1[0]->任务=$arr2(不要同时更改两者)。

任何时候当您提到您遇到错误时,请包括您收到的实际错误。那是什么。。。为什么需要将对象编码为json并解码回数组?不能在$result1[0]->task=$result2@user3522457
$result1
首先应该是一个数组,最后只需使用
json\u encode
。。。。