I can't seem to figure out how to get a JS array into PHP.
What I have to work with looks like this:
var arrLow = [
{
"e": "495864",
"rank": "8678591",
"rankmove": "<p><img src='up.php?uStyle=144'> UP 495864"
},
{
"e": "104956",
"rank": "-",
"rankmove": "<p><img src='up.php?uStyle=145'> DOWN 1"
},
{
"e": "0",
"rank": "0",
"rankmove": "<p><img src='up.php?uStyle=975'> NEW"
}
]
json_decode and others just return NULL, google only returns some strange way to use serialize() with a HTTP POST from a JS-understanding browser which really can't work here
Does anyone have any clue how :x
==========================================================================
edit: Thanks guys! Didnt know it was so easy
<?php
$json = file_get_contents('24d29b1c099a719zr8f32ce219489cee.js');
$json = str_replace('var arrLow = ','' ,$json);
$data = json_decode($json);
echo $data[0]->e;
?>
I can't seem to figure out how to get a JS array into PHP.
What I have to work with looks like this:
var arrLow = [
{
"e": "495864",
"rank": "8678591",
"rankmove": "<p><img src='up.php?uStyle=144'> UP 495864"
},
{
"e": "104956",
"rank": "-",
"rankmove": "<p><img src='up.php?uStyle=145'> DOWN 1"
},
{
"e": "0",
"rank": "0",
"rankmove": "<p><img src='up.php?uStyle=975'> NEW"
}
]
json_decode and others just return NULL, google only returns some strange way to use serialize() with a HTTP POST from a JS-understanding browser which really can't work here
Does anyone have any clue how :x
==========================================================================
edit: Thanks guys! Didnt know it was so easy
<?php
$json = file_get_contents('24d29b1c099a719zr8f32ce219489cee.js');
$json = str_replace('var arrLow = ','' ,$json);
$data = json_decode($json);
echo $data[0]->e;
?>
Share
Improve this question
edited Oct 7, 2010 at 22:48
jen
asked Oct 7, 2010 at 22:26
jenjen
1751 gold badge2 silver badges6 bronze badges
3
- Are you trying to copy/paste that array into PHP? Generally any JS array that you send to PHP will be serialized in one form or another. Can you give an example of how you're trying to use it? – mway Commented Oct 7, 2010 at 22:29
- A cron job (not under my control) grabs it and saves it as example.js. It changes every hour, and i'm just reading it into PHP then trying to parse it. It basically appears as a plaintext file, and I can only work with PHP (or bash) – jen Commented Oct 7, 2010 at 22:33
- you may try this : var_dump(json_decode($arrLow)); where $arrLow is sent from javascript, normally in a form or by AJAX. – Michael Mao Commented Oct 7, 2010 at 22:33
4 Answers
Reset to default 8You can use json_decode()
for this. The trick is to leave away the var arrLow =
part (so that only the array itself is left). You can assign the value to the php variable $arrLow
like this:
$js = '[ {"e" : "495864", ...';
$arrLow = json_decode($js);
A quick'n'dirty hack to remove the beginning would be to use the strstr()
function.
$js = strstr('var arrLow = [ {..', '[');
2 options:
- Just remove the
var arrLow =
at the front (might need a regex if its variable), and parse as json. - Go for full on javascript parsing
//define javascript array
>var mainArray = {};
> // inner loops
mainArraySub['XXX'] = [];
mainArray = JSON.stringify(mainArray);
passing javascript array content in jquery ajax request as
$.ajax({
type: "POST",
data:{paramval:mainArray},
dataType: "json",
url: url,
success: function(msg){
}
});
in POST request you will get as below
{"row1":{"0":{"nnnn":"aaaa"},"1":{"Movie":"aaaa"},...}
// in a.php file call as below
$Info = $_REQUEST['rowdata'];
$tre = json_decode(stripslashes($Info),true);
var_dump($tre);
According to the JSONLint validator this is valid JSON (without the var arrLow =
). So any good json_decode()
implementation should do the trick. Maybe the implementation you are using has certain limitations? The JSON website has a neat list of links to implementations of json_decode in PHP (and lots of other languages too). You can be sure to find one that does work.