Solved

Converting arrays to JSON and back again

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

747 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

18 Experts available now in Live!

Get 1:1 Help Now