php - array to string conversion return error of stdClass -
if($stmt->execute()){ $user = $stmt->get_result(); while ($obj = $user->fetch_object()) { $result[] = $obj; } } $result1encoded = json_encode($result); echo $result1encoded; // [{"uid":"1","firstname":"john"}]
i used implode :
echo $result1encoded = implode(' ',$result1); // expecting '[{"uid":"1","firstname":"john"}]'
but says
object of class stdclass not converted string
you can use array_map("json_encode", $your_array)
first convert each element of array string, , use implode
glue together.
<?php $a = array(); $a[0] = new stdclass(); $a[0]->uid = "1"; $a[0]->firstname = "john"; $a[1] = new stdclass(); $a[1]->uid = "2"; $a[1]->firstname = "albert"; $b = array_map("json_encode", $a); echo implode(' ', $b); ?>
Comments
Post a Comment