Reputation: 11
I have a JS array, which I convert to JSON
JS
mycode[0][0]=true
mycode[0][1]="element1"
mycode[0][2]=400
mycode[0][3]=150
mycode[0][4]=148
mycode[0][5]=148
turned into JSON:
[
[
true,
"element1",
400,
150,
148,
148
]
]
Now I push this to PHP
PHP code:
$decoded = json_decode($_GET["q"]);
$response=$q[0];
echo $response;
and it outputs a letter or a symbol as JSON was a string.
If I use $decoded[0][0] or $decoded[0] instead of $q[0] I get nothing...
What am I actually doing wrong?
What do I want? I need to have the same array I had in JS, just in PHP (the array will later be used in a PHP function)
Upvotes: 1
Views: 143
Reputation: 11
Sorry but in my opinion you need to read a lot more about the three things you want to use, specially AJAX and JSON
Upvotes: 0
Reputation: 799014
Code:
<?php
$json = '[[ true,"element1",400,150,148,148 ]]';
$dec = json_decode($json);
var_dump($dec);
?>
Output:
array(1) {
[0]=>
array(6) {
[0]=>
bool(true)
[1]=>
string(8) "element1"
[2]=>
int(400)
[3]=>
int(150)
[4]=>
int(148)
[5]=>
int(148)
}
}
Works fine here. Your problem must be somewhere else.
$ php -v
PHP 5.3.2 (cli) (built: Apr 27 2010 17:55:48)
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
Upvotes: 4
Reputation: 134207
How are you pushing your JSON to the PHP code. When you pass your JSON to the PHP back-end, you should use a library such as json2.js to encode the content:
var myJSONText = JSON.stringify(myObject, replacer);
You may also need to URL-encode the JSON if you are passing it using GET.
Upvotes: 0