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?
-
1If you want to convert the same object in PHP then use php.net/manual/en/function.json-decode.php otherwise see mata's answer.Amir Raminfar– Amir Raminfar2012年05月07日 21:05:03 +00:00Commented 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?Taha Paksu– Taha Paksu2012年05月07日 21:05:49 +00:00Commented 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 :)Sir– Sir2012年05月07日 21:11:09 +00:00Commented May 7, 2012 at 21:11
-
Ok then. Just to clarify the question. Good luck!Taha Paksu– Taha Paksu2012年05月07日 21:13:17 +00:00Commented May 7, 2012 at 21:13
4 Answers 4
$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")
)
);
Comments
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
check out multidimensional arrays :
Comments
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