0
    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 like this :

    echo $result1encoded = implode(' ',$result1);
// expecting '[{"uId":"1","firstName":"John"}]'

But it says

Object of class stdClass could not be converted to string 
Harshana Narangoda
  • 775
  • 1
  • 8
  • 23

2 Answers2

0

To convert Array to String you can use serialize() method like this

$result1encoded = json_encode($result);

echo serialize($result1encoded);

Another Way to do is

 $result1encoded = json_encode($result);

echo $result1 = implode(' ',$result1encoded);


|**Edited Part**|

Please go through flowing link hope it solves ur problem Array to String Conversions

Community
  • 1
  • 1
alex
  • 518
  • 3
  • 10
  • your first solution I got something like s59 in front of it. your second suggestion didnt work. – user3522462 Apr 25 '14 at 05:26
  • go through that link that I have edited now I hope u can solve your solutions Thanks – alex Apr 25 '14 at 05:53
  • I did it and I got the flowing out put by the second method I use like this `echo $result1 = implode(" ", $result1encoded);` and the out puts are `[{"id":"1","ProductType":"iPhone"},{"id":"2","ProductType":"iPad"},{"id":"3","ProductType":"iMac"},{"id":"4","ProductType":"Accessories"}]` – alex Apr 25 '14 at 06:41
0

You can use array_map("json_encode", $your_array) to first convert each element of the array to string, and then use implode to glue it together.

See this https://eval.in/141541

<?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);

?>
SajithNair
  • 3,867
  • 1
  • 17
  • 23