1

I have a JS script which im converting the code over to PHP but am stuck on what the correct syntax is to create the same as this piece of code in JS in my PHP script:

var sdata = {
           "13":{
               "22":["618","22","1","1","0","0","0"],
               "21":["617","22","1","1","0","0","0"]
           },               
           "14":{
                "22":["619","22","1","1","0","0","0"],
                "20":["622","22","1","1","0","0","0"]
           },    
           "15":{
                "20":["623","22","1","1","0","0","0"]
     }
};

Any ideas?

4
  • 1
    If you want to convert the same object in PHP then use php.net/manual/en/function.json-decode.php otherwise see mata's answer. Commented May 7, 2012 at 21:05
  • The wrapper is an object, and the first levels are also objects and their children are arrays. Do you want the exact same structure, or you just want the data layer to be the same? Commented May 7, 2012 at 21:05
  • @tpaksu i wanted it like mata answered :) im not parsing the result of the variable - i'm re-writing the entire script :) Commented May 7, 2012 at 21:11
  • Ok then. Just to clarify the question. Good luck! Commented May 7, 2012 at 21:13

4 Answers 4

2
$sdata = array(
           "13" => array(
               "22" => array("618","22","1","1","0","0","0"),
               "21" => array("617","22","1","1","0","0","0")
           ),               
           "14" => array(
                "22" => array("619","22","1","1","0","0","0"),
                "20" => array("622","22","1","1","0","0","0")
           ),    
           "15" => array(
                "20" => array("623","22","1","1","0","0","0")
     )
);
Sign up to request clarification or add additional context in comments.

Comments

2

The easiest thing to do and to be sure you're exact is to use the jQuery .get function, send this array over as part of the data, then run the PHP function json_decode on the $_GET variable to get the PHP array :) You can then print_r the array if you need to hardcode it.

2 Comments

true but i think they are looking for the actual coding process - not consuming JSON
Well, my idea was to consume the JSON then echo the PHP array. This way you preclude any errors creeping in by doing it manually.
0

check out multidimensional arrays :

http://php.net/manual/en/language.types.array.php

Comments

0

It looks like what you need is a multi-dimensional array. Since you seem to be new to PHP, check out the code examples on the array manual page from PHP: http://php.net/manual/en/language.types.array.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.