Solved

Converting arrays to JSON and back again

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How do I speed up this PDO query 4 41
SSL unsecure page mystery 17 47
Undefined variable with $_POST in PHP 5 40
Phone number mask 4 14
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

751 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