PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Closed Thread
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 9th, 2013, 01:48 PM
richpri's Avatar
richpri richpri is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Location: Chicago
Posts: 49 richpri User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 h 27 m 23 sec
Reputation Power: 1
Facebook
Question PHP-General - Problem with output of json_decode

I am trying to pass input to a PHP program via AJAX. The resulting array is not what I expected. A sub array gets mangled as can be seen in the test command line program below.


The test PHP program looks like this:

Code:
#!/usr/bin/php
<?php
$newgame = '{"gname": "game name","boxid": "7","players": [{"0": "pname0"},{"1": "pname1"},{"2": "pname2"}]}';
$game = json_decode($newgame, true);
$textgame = json_encode($game);
$errorgame = json_last_error();
var_dump($newgame);
var_dump($textgame);
var_dump($errorgame);
?>


And the output looks like this:


Code:
#!/usr/bin/php
string(96) "{"gname": "game name","boxid": "7","players": [{"0": "pname0"},{"1": "pname1"},{"2": "pname2"}]}"
string(86) "{"gname":"game name","boxid":"7","players":[["pname0"],{"1":"pname1"},{"2":"pname2"}]}"
int(0)


As you can see, decoding and encoding returns a different and erronious string.
I would appreciate any advice on the format of the newgame string or any other comments on this problem.

Reply With Quote
  #2  
Old January 9th, 2013, 02:05 PM
requinix's Avatar
requinix requinix is offline
Still alive
Click here for more information.
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,690 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 4 Days 3 h 43 m 47 sec
Reputation Power: 8969
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
It's not erroneous: json_decode() with $assoc=true is potentially lossy. Arguably not though because {"0":"pname0"} and ["pname0"] will behave nearly identically in Javascript.

Pass $assoc=false.
PHP Code:
 $game json_decode($newgame); 
Comments on this post
richpri agrees: Good point.

Reply With Quote
  #3  
Old January 9th, 2013, 02:59 PM
richpri's Avatar
richpri richpri is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Location: Chicago
Posts: 49 richpri User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 h 27 m 23 sec
Reputation Power: 1
Facebook
Smile

I found a string that works.
Here is the new test output from same program with new string:
Code:
string(77) "{"gname": "game name","boxid": "7","players": ["pname0", "pname1", "pname2"]}"
string(72) "{"gname":"game name","boxid":"7","players":["pname0","pname1","pname2"]}"
int(0)

The strings are the same except for whitespace.

Reply With Quote
  #4  
Old January 9th, 2013, 03:33 PM
ManiacDan's Avatar
ManiacDan ManiacDan is offline
Likely to be eaten by a grue.
Dev Shed God 10th Plane (9500 - 9999 posts)
 
Join Date: Oct 2006
Location: Pennsylvania, USA
Posts: 9,801 ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)  Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 2 Months 3 Weeks 16 h 59 m 20 sec
Reputation Power: 6112
I'm not sure what the problem even is here. The objects are still the same, regardless of what the keys end up being. Since JS doesn't support string keys, the whole exercise is kind of moot.

Encoding and decoding the string you gave us produces the same array:



PHP Code:
 $newgame '{"gname": "game name","boxid": "7","players": [{"0": "pname0"},{"1": "pname1"},{"2": "pname2"}]}';
$game json_decode($newgametrue);
$textgame json_encode($game);
$errorgame json_last_error();
print_r($game);
print_r(json_decode($textgame,true)); 
Produces:
Code:
Array
(
    [gname] => game name
    [boxid] => 7
    [players] => Array
        (
            [0] => Array
                (
                    [0] => pname0
                )

            [1] => Array
                (
                    [1] => pname1
                )

            [2] => Array
                (
                    [2] => pname2
                )

        )

)

Array
(
    [gname] => game name
    [boxid] => 7
    [players] => Array
        (
            [0] => Array
                (
                    [0] => pname0
                )

            [1] => Array
                (
                    [1] => pname1
                )

            [2] => Array
                (
                    [2] => pname2
                )

        )

)
Comments on this post
richpri agrees: The second version works for me. My problem has been solved.
__________________
HEY! YOU! Read the New User Guide and Forum Rules

"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

"The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

Reply With Quote
Closed Thread

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > PHP-General - Problem with output of json_decode

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap