I have an array in Javascript which is passed to a PHP script via Ajax.
In file.js:
var params = {};
params["apples"] = "five";
params["oranges"] = "six";
params["pears"] = "nine";
var ajaxData = {data : params};
fetchData(ajaxData);
function fetchData(arg) {
$.ajaxSetup ({
cache: false
});
request = $.ajax ({
type: "GET",
url: "script.php",
data: arg,
});
request.done(function(response){
$("#somediv").text(response);
});
request.fail(function (jqXHR, textStatus, errorThrown){
// log the error to the console
console.error(
"The following error occured: "+
textStatus, errorThrown
);
});
}
In script.php:
<?php
$data = json_decode(stripslashes($_GET['data']));
var_dump($data);
?>
The result of the var_dump is:
object(stdClass)#1 (3) { ["apples"]=> string(4) "five" ["oranges"]=> string(3) "six" ["pears"]=> string(4) "nine" }
But I don't want to use an object, I want to use it the same way I used the data in Javascript (ie: being able to do:
$apples = $data["apples"]
Is there any way to deal with this data as an array and not an object right from the get-go? If not, how do I "convert" what I have now into the same associative array as I had in Javascript?
Thanks.