Parsing javascript arrays in PHP

I can't figure out how to get a JS array in PHP.

I need to work as follows:

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 returns a weird way to use serialize () with HTTP POST from a JS understanding browser that really cannot work here.

Does anyone know how: x

==================================================== ==========================

Edit: Thanks guys! Didn't know it was that easy

 <?php $json = file_get_contents('24d29b1c099a719zr8f32ce219489cee.js'); $json = str_replace('var arrLow = ','' ,$json); $data = json_decode($json); echo $data[0]->e; ?> 
+7
javascript arrays php
source share
4 answers

You can use json_decode() . The trick is to leave part of var arrLow = (so that only the array itself remains). You can assign the value to the php $arrLow as follows:

 $js = '[ {"e" : "495864", ...'; $arrLow = json_decode($js); 

A quick dirty hack to remove the start would be to use the strstr() function.

 $js = strstr('var arrLow = [ {..', '['); 
+8
source share

2 options:

  • Just remove var arrLow = in front (a regular expression may be required if its a variable) and parse as json.
  • Go all the way to javascript analysis
+1
source share

According to the JSONLint validator, this is valid JSON (without var arrLow = ). Therefore, any good json_decode() implementation should do the trick. Maybe the implementation you are using has certain limitations? The JSON.org website has a neat list of links to json_decode implementations in PHP (and many other languages). You can be sure to find one that works.

0
source share
 //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); 
0
source share

All Articles