Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
325 views
in Technique[技术] by (71.8m points)

php - convert array that contains object to string

I got an error when I did $result1 = implode(' ',$result1)

I use 2 query and wish to combine the result into one, here's my query:

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;
    }

}
  • convert $result1 and result2 to string here
  • I need $result1 and result2 to be like

    '[{ "uId":"1", "firstName":"James", "lastName":"Bond" }]'

so that it can run below code.

$arr1 = json_decode($result1,true);
$arr2 = json_decode($result2,true);
$arr1[0]['task'] = $arr2;
$finalJSON = json_encode($arr1);
echo $finalJSON;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

$result1 and $result2 are already arrays.

Remove the below line:

$arr1 = json_decode($result1,true);
$arr2 = json_decode($result2,true);

And either change fetch_object to fetch_array,

or change $arr1[0]['task'] = $arr2; to $arr1[0]->task = $arr2; (don't change both).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...