php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #42270 stdClass could not be converted to string
Submitted: 2007-08-11 06:13 UTC Modified: 2007-08-17 19:57 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: steven dot mccoy at miru dot hk Assigned:
Status: Not a bug Package: JSON related
PHP Version: 5.2.4RC1-dev OS: Ubuntu 7.04
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: steven dot mccoy at miru dot hk
New email:
PHP Version: OS:

 

 [2007-08-11 06:13 UTC] steven dot mccoy at miru dot hk
Description:
------------
This might be related to bug 41221, upgrading from PHP 5.1.6-1 (Debian) to 5.2.1 (Ubuntu) created a regression in handling output of JSON decoding.

Does this mean that I can only use arrays ( json_decode($json, true) ), and have to update all code appropriately?

Reproduce code:
---------------
<?php
$array = array(
                "first" => array(array("second" => 2), "two", "three"),
                );
$json = json_encode($array);
$obj = json_decode($json);
var_dump(array_diff($array,$obj->first));
?>


Expected result:
----------------
array(1) {
  ["first"]=>
  array(3) {
    [0]=>
    array(1) {
      ["second"]=>
      int(2)
    }
    [1]=>
    string(3) "two"
    [2]=>
    string(5) "three"
  }
}


Actual result:
--------------
Catchable fatal error: Object of class stdClass could not be converted to string in /tmp/- on line 7


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-08-12 06:54 UTC] steven dot mccoy at miru dot hk
Reproduced with the Win32 CVS build:

PHP 5.2.4RC1-dev (cli) (built: Aug 12 2007 00:04:25)
Copyright (c) 1997-2007 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies
 [2007-08-13 21:34 UTC] crrodriguez at suse dot de
Workaround : use array_udiff with a supplied callback function or use only arrays.
 [2007-08-16 11:46 UTC] jani@php.net
See also bug #42303
 [2007-08-16 11:52 UTC] jani@php.net
Here's what I get for the encode:

{"first":[{"second":2},"two","three"]}

So actually you have another object inside the first, here's var_dump($obj);

object(stdClass)#1 (1) {
  ["first"]=>
  array(3) {
    [0]=>
    object(stdClass)#2 (1) {
      ["second"]=>
      int(2)
    }
    [1]=>
    string(3) "two"
    [2]=>
    string(5) "three"
  }
}

And using json_decode($json, true):

array(1) {
  ["first"]=>
  array(3) {
    [0]=>
    array(1) {
      ["second"]=>
      int(2)
    }
    [1]=>
    string(3) "two"
    [2]=>
    string(5) "three"
  }
}

So what's the bug?
 [2007-08-16 12:11 UTC] steven dot mccoy at miru dot hk
The situation I'm using it in is where the browser sends an array of objects to PHP.  The PHP scripts uses PEAR::DB_DataObject to generate an array of objects inside a database table, I then use array_diff/array_udiff to calculate what is new, what has changed, and what should be deleted.  The array_diff for items that have not changed fails in 5.2 because PHP fails be able to compare some of the stdClass objects.

 $new_products = array_filter($products, array('Order','is_new_product'));
 debug('new_order_products: '.count($new_products).' found');
 $old_products = array_diff($new_products, $products);
 debug('old_order_products: '.count($old_products).' calculated');
 $updated_products = array_udiff($old_products, $current_products, array('Order', 'product_compare'));
 debug('updated_order_products: '.count($updated_products).' found');
 $deleted_products = array_udiff($current_products, $products, array('Order', 'product_compare_by_id'));
 debug('deleted_order_products: '.count($deleted_products).' found');


PEAR::DB_DataObject returns table rows as objects, the PHP JSON module defaults to objects, but can include arrays of objects as shown previously.

As a workaround I rebuilt PHP 5.1.6 from Ubuntu 6.10 on 7.04.  If someone can provide a array_udiff function that can compare objects like before that would be great :-)

If the current circumstances are not clear, I will repeat:  PHP 5.1.6 generates the expected result output as shown, PHP 5.2 does not.
 [2007-08-17 13:27 UTC] jani@php.net
So this has nothing to do with JSON? Please reclassify if that is true and fix the summary to contain what the bug is really about..
 [2007-08-17 14:59 UTC] steven dot mccoy at miru dot hk
Well its either json_decode not making stdClass objects friendly for comparison or array_diff() that broke.  Lets try the latter then.
 [2007-08-17 19:57 UTC] jani@php.net
Okay, so you think array_diff() is broken. As our bug system sucks, can't edit original comment, etc. so please, open new short report about the array_diff issue. (this one is a bit confusing with the json stuff and all) And make sure the new report does not have any references to JSON. :D
 [2010-12-27 08:24 UTC] serg4172 at mail dot ru
This is really json problem. Although I have PHP 5.3.3 I have the same issue.

$params = json_decode($options);

This produce error

Object of class stdClass could not be converted to string

i solved i like this

$params = json_decode(trim(str_replace("\r\n", '\\n', $options)));
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 01:01:30 2024 UTC