Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Converting arrays to JSON and back again

Posted on 2009-04-02
5
Medium Priority
?
685 Views
Last Modified: 2013-12-13
I'm doing some experimentation with storing values in JSON, specifically arrays.

For simple, for one dimensional arrays this is not too difficult:

$json = json_encode($array) converts the array to JSON and then to get the array back again you do:

$object = json_decode($json); $array = get_object_vars($object);

However, this becomes more complicated with multidimensional arrays. Encoding is not a problem, but the decode process does not work the same. json_decode creates a multidimensional object (is there such a thing as a multidimensional object?). At any rate, get_object_values leaves objects as objects in the array and you might end up with something like:

Array ( [what] => is [this] => was [number] => 8 [array] => stdClass Object ( [second] => level ) )

My question is: Is there a way to deal with this easily? Or will I need to build recursive function myself that reads through all the values of the object to convert them to array key=>value pairs, testing for objects as it goes and then calling itself to convert embedded objects?
0
Comment
Question by:Hube02
  • 3
5 Comments
 
LVL 4

Expert Comment

by:davidsperling
ID: 24049076
Maybe Zend Framework's JSON classes can do it?

http://framework.zend.com/manual/en/zend.json.html

You can use (almost) any part of ZF separatly.
http://framework.zend.com
0
 
LVL 18

Author Comment

by:Hube02
ID: 24049268
From what I read there, this framework simply uses json_encode/json_decode function and works not differently, you still end up with an object when it is decoded and not an array. Unless I missed something there, please point it out if I did.
0
 
LVL 11

Accepted Solution

by:
bansidhar earned 2000 total points
ID: 24049480
you can simply use

$array = json_decode($json,true);
0
 
LVL 18

Author Comment

by:Hube02
ID: 24049627
Thank you much, I missed the use of that second parameter. I knew there had to be something simple I was overlooking.
0
 
LVL 18

Author Closing Comment

by:Hube02
ID: 31565753
Thanks again
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question