Object of class stdClass could not be converted to string- can't find a solution

14,977

Solution 1

Like an array, you can't echo a stdClass object. You could var_dump($array_diff) and echo specific properties like so:

echo $array_diff->my_property;

Solution 2

After $array_diff = json_decode($string_diff);, $array_diff is an object, so you can't just echo it. Try something like

echo $array_diff->your_property;
Share:
14,977
Mihai Bujanca
Author by

Mihai Bujanca

Computer Vision researcher; C++, OpenCV, Python

Updated on June 05, 2022

Comments

  • Mihai Bujanca
    Mihai Bujanca almost 2 years

    I have the following code:

    if ($difference)
    {
        $.post("recover_functions/upload_photos.php",
               {upload:$difference},
               function(response)
               {
                   alert($difference);
                   if (response.status)
                       alert("Successfully uploaded photos");
                   else
                       alert(response);
                });
    }
    

    The alert will display:

    {"difference":"[{\"aid\":\"100000543443572_1073741825\",\"backdated_time\":null,\"caption\":\"\",\"link\":\"http:\\\/\\\/www.facebook.com\\\/photo.php?fbid=614604095234366&set=a.614604001901042.1073741825.100000543443572&type=1\",\"pid\":\"100000543443572_2384218\",\"place_id\":null}]"}
    

    And I get the `POST request like this:

    $string_diff = $_POST['upload'];
    $array_diff  = json_decode($string_diff);
    
    echo $array_diff;
    

    Not the response is Object of class stdClass could not be converted to string. Please help, I have a deadline (1 hour) and I can't get this working.

  • Sherlock
    Sherlock almost 11 years
    You can if it implements toString.
  • hek2mgl
    hek2mgl almost 11 years
    @Sherlock StdClass doesn't implement __toString
  • Sherlock
    Sherlock almost 11 years
    I was making a general comment on his statements that objects can't be echoed.
  • Terry Harvey
    Terry Harvey almost 11 years
    @Sherlock Sorry, you're right. I edited my post - should have been more specific.