I'm facing a problem that I'm trying to solve for few hours already, I hope that you can help me out. So from the beginning, I have an algorithm in JavaScript that generating variables which I want to save to database and print to user on website. To do that I thought that I will generate an array of object to handle all variables at once (it generates as many variables as user want, so sometimes it can be just few, sometimes few times more). I thought that it will be easy to send and handle with json and then be accessable as normal variables in PHP. So here is my test data:
[[{"id_day":1},{"exercise":"some test exercise1"},{"repeats":2},{"energyWorth":300}],
[{"id_day":1},{"exercise":"some test exercise2"},{"repeats":2},{"energyWorth":300}],
[{"id_day":2},{"exercise":"some test exercise3"},{"repeats":2},{"energyWorth":300}],
[{"id_day":2},{"exercise":"some test exercise4"},{"repeats":2},{"energyWorth":300}],
[{"id_day":3},{"exercise":"some test exercise5"},{"repeats":2},{"energyWorth":300}],
[{"id_day":3},{"exercise":"some test exercise6"},{"repeats":2},{"energyWorth":300}],
[{"id_day":4},{"exercise":"some test exercise7"},{"repeats":2},{"energyWorth":300}],
[{"id_day":4},{"exercise":"some test exercise8"},{"repeats":2},{"energyWorth":300}],
[{"id_day":5},{"exercise":"some test exercise9"},{"repeats":2},{"energyWorth":300}],
[{"id_day":5},{"exercise":"some test exercise10"},{"repeats":2},{"energyWorth":300}],
[{"id_day":6},{"exercise":"some test exercise11"},{"repeats":2},{"energyWorth":300}],
[{"id_day":6},{"exercise":"some test exercise12"},{"repeats":2},{"energyWorth":300}]]
I have used ajax in my script to send this array of objects by this code:
$.ajax({
url: 'test2.php',
type: 'post',
data: {"data_js" : JSON.stringify(plan)},
success: function(data) {
}
});
And wanted to use em now in PHP file called test2.php by running code:
if (isset($_POST["data_js"])) {
$data_js = $_POST["data_js"];
$data_js = json_decode($data_js);
var_dump($data_js);
var_dump($data_js[0][1]);
}
So I have successfully sent my array of objects to variable called $data_js and decoded em with json to use em in PHP. The problem is that my array looks like this now:
array(20) {
[0]=>
array(4) {
[0]=>
object(stdClass)#1 (1) {
["id_day"]=>
int(1)
}
[1]=>
object(stdClass)#2 (1) {
["exercise"]=>
string(37) "some test exercise1"
}
[2]=>
object(stdClass)#3 (1) {
["repeats"]=>
int(2)
}
[3]=>
object(stdClass)#4 (1) {
["energyWorth"]=>
int(300)
}
}
[1]=>
array(4) {
[0]=>
object(stdClass)#5 (1) {
["id_day"]=>
int(1)
}
[1]=>
object(stdClass)#6 (1) {
["exercise"]=>
string(38) "some test exercise2"
}
[2]=>
object(stdClass)#7 (1) {
["repeats"]=>
int(2)
}
[3]=>
object(stdClass)#8 (1) {
["energyWorth"]=>
int(300)
}
}
and so on. The thing is that I have to access to each variable like in JS I could run data[0].exercise and it gave me simple output "some test exercise1". I was trying to use for example var_dump($data_js[0][1]); which gives me variable I want to get but in this format:
object(stdClass)#2 (1) {
["exercise"]=>
string(37) "some test exercise1"
}
And it is far from what I want to get. I might be using wrong methods, doing this for the first time so if you guys have ideas or tell me that I m just doing it wrong I would be more than happy. Seems that I got lost in this whole JS-JSON-PHP thing. Thank you guys in advance!