Snupas Snupas - 5 months ago 15
Javascript Question

Transferring hash maps over ajax and converting to php array

I'm making an interpreter for a mini language, and I'm storing related information in hash maps. I'm convering those hash maps with JSON stringify, to send them over via ajax to the server side.

This is is the post-JSON stringify code I'm sending over to the server side:


On the server side, how could I easily make a php array out of the
bit without searching for certain characters?

Before sending it over ajax the output of json array is:


After receiving it, the data is:


after the
function, it becomes:


that gives me null

The ajax code:

function addValues(jsonArray) {



url: 'insertTree.php',
type: 'POST',
data: 'dataToReceive=' + jsonArray,
success: function(data) {
//called when successful
error: function(e) {
//called when there is an error


the php receiving code:

$dataReceived = htmlspecialchars(strip_tags($_POST["dataToReceive"]));

$dataRefined = htmlspecialchars_decode($dataReceived);

$var = json_decode($dataRefined, true);

var_dump($var['kv']); //null


Use json_decode to get the value. Since your base string isn't valid JSON, I massaged it a bit to get it to be

$string = '{"kv":["24","23","20"],"interface":"dropdown"},{"ma":["2","3","4"],"interface":"button"}';
$data = explode('|', str_replace('},{', '}|{', $string));

foreach($data as $str) {
    $var = json_decode($str, true);
    if(isset($var['kv'])) var_dump($var['kv']);