-1

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!

Sinner
  • 73
  • 7
  • 2
    `json_decode($data_js, true);` – Matteo Tassinari Nov 27 '17 at 19:13
  • `var_dump($data_js[0][1]->exercise);` ? – splash58 Nov 27 '17 at 19:15
  • Why do you not generate a more suitable array as `[{"id_day":1,"exercise":"some test exercise1","repeats":2,"energyWorth":300},..` In that case it will be `$data_js[$i]->exercise` ... – splash58 Nov 27 '17 at 19:17
  • Matteo seems like I can do this by it and then: $var1 = $data[0][1]; echo implode("",$var1);, but not sure if it's a good practise @splash58 Sadly, doesn't work, but as you said, Idk why I didn't generate array like you say (my mistake, will fix it and try your method) – Sinner Nov 27 '17 at 19:22
  • @Sinner what do you mean "don't work" ? - https://eval.in/908699 – splash58 Nov 27 '17 at 19:27
  • @splash58 It works as var_dump($data_js[0]->exercise);, but it prints string(37) "some test exercise1" and I need clear ""some test exercise1"" to use it as variable – Sinner Nov 27 '17 at 19:39
  • @Sinner `$var = $data_js[0]->exercise;` Is this your first code on php? – splash58 Nov 27 '17 at 19:40
  • @splash58 damn! just my brain went off, thank you a lot :) – Sinner Nov 27 '17 at 19:44

1 Answers1

1

You should use json_decode($data, true);

The second parameter assures that data will be decoded in array format.