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
。。。。