Solved

Converting arrays to JSON and back again

Posted on 2009-04-02
5
680 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

622 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