Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
327 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 Answer

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...