On 27/02/07, Matthew Weier O'Phinney <[EMAIL PROTECTED]> wrote:
-- Dmitry Shirokov <[EMAIL PROTECTED]> wrote
(on Tuesday, 27 February 2007, 04:11 PM +0000):
> hello guys, i had weird problem with code like that (zf 0.7.0):
>
> foreach ($this->_subSites->fetchAll() as $subSite)
> {
> $resRow = new StdClass;
>
> $resRow->id = $subSite->id;
>
> $resRow->subDomain = $subSite->subDomain;
>
> $resRow->image = $subSite->image;
>
> $res[] = $resRow;
>
> }
>
> echo Zend_Json::encode($res);
>
> on my home machine with php 5.2.0 return:
>
> [{"id":"19","subDomain":"one","image":"\/images\/sub\/image1.jpg"},
> {"id":"13","subDomain":"test","image":"\/images\/sub\/ee.jpg"},
> {"id":"1","subDomain":"two","image":"\/images\/sub\/test.jpg"}]
>
> on the server with php 5.1.4:
>
[{"__className":"stdClass","id":"19","subDomain":"one","image":"/images/sub/
> image1.jpg
"},{"__className":"stdClass","id":"13","subDomain":"test","image":"/
> images/sub/ee.jpg"},
>
{"__className":"stdClass","id":"1","subDomain":"two","image":"/images/sub/
> test.jpg"}]
>
> not same...
>
>
> any ideas?
When encoding objects with Zend_Json, it adds an extra key at the
beginning indicating the class name; this is done to hint to the JS
consumer a JS class to instantiate (should that be desired).
Now, that said, with ZF release 0.7.0, a change was made to use ext/json
if detected. ext/json basically simply casts objects to arrays prior to
encoding them -- which is why the two versions are different.
If you want the to function the same for your example, do one of the
following:
* cast objects to arrays prior to encoding:
Zend_Json::encode((array) $res);
* or use Zend_Json's encoder explicitly:
Zend_Json_Encoder::encode($res);
(this latter is true of the decoder, too)
--
Matthew Weier O'Phinney
PHP Developer | [EMAIL PROTECTED]
Zend - The PHP Company | http://www.zend.com/
Thanks man.
--
Thanks, Dmitry