You can use the json_decode($yourArray) function. It can be used to convert JSON to PHP arrays.
Let me explain (I won't be mad like some other people, even though I found an answer in 30 seconds):
You can type a script and add the JSON object in there. Then, you will set a cookie to the object stringified:
<script>
var json = {"one":1, "two":2};
var jsonStr = JSON.stringify(json);
document.cookie("json="+jsonStr);
</script>
No matter how you ended up with the JSON object, you can do this in any way.
Then, you dump it in PHP, if the cookie is set. Else, reload the page so that the cookie is set:
<?php
if (!isset($_COOKIE["json"])){
//Check whether the cookie is set. If not so, then reload the document:
echo "<script>location.reload();</script>";
exit; //Exit is because you don't want actions in PHP to continue after the page has been set to be reloaded
}
else{
//But, if the cookie is set, then set a variable to it
$json = $_COOKIE["json"];
}
$decoded = json_decode($json, true); //Decode the object. You should type "true" because else it will be converted to an std_class... I didn't search any more on this
var_dump($decoded); //See the output
?>
Output:
array(2) { ["one"]=> int(1) ["two"]=> int(2) }
This would solve the first version of your question, but you changed it and wanted an array of split objects. Like this:
{
"one":1,
"two":2
}
To be converted to this:
[
{"one":1},
{"two":1}
]
I would ask why, but let me just solve this.
Remember the script we wrote? Let's add more to it.
<script>
//Now you wanted for each key on the object to be added into an array. I made a function for this:
function objectToArray(object){
var retArray = [];
var allKeys = Object.keys(object);
for (i = 0; i < allKeys.length; i++){
//For each key of the object:
toBeAdded = allKeys[i];
toBeAddedObj = {};
toBeAddedObj[toBeAdded] = object[toBeAdded];
retArray.push(toBeAddedObj);
}
return retArray;
}
var json = {"one":1, "two":2};
json = objectToArray(json);
var jsonStr = JSON.stringify(json);
document.cookie("json="+jsonStr);
</script>
So what exactly does this function do? For each key of the object, it pushes it into the array. Keep your PHP the same.