Solved

Converting arrays to JSON and back again

Posted on 2009-04-02
5
674 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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
This article discusses how to create an extensible mechanism for linked drop downs.
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now