1
if (isset($_POST['myData'])) {
    $json = json_decode($_POST['myData'], true);
    global $phone, $name, $id_proj;
    $arr = array();
    foreach ($json as $item => $k) {
        $id_proj = $k['movieid'];
        $name = $k['name'];
        $phone = $k['phone'];
        array_push($arr, $k['sedloid']);
    }
    echo "Output $id_proj AND $name AND $phone ";
}

I am sending from reservation page some information about user.

This is the JSON I'm sending at PHP PAGE:
myData:[{"movieid":"1"},{"name":"Random Name"},{"phone":"0601234567"},{"sedloid":"6"},{"sedloid":"7"},{"sedloid":"8"}]

As the response, I've got Undefined index error, multiple times for every row in for each loop.

This is VAR_DUMP result from decoded JSON value:

array (size=6)
  0 =>
    array (size=1)
      'movieid' => string '1' (length=1)
  1 =>
    array (size=1)
      'name' => string 'Random Name' (length=11)
  2 =>
    array (size=1)
      'phone' => string '0601234567' (length=10)
  3 =>
    array (size=1)
      'sedloid' => string '6' (length=1)
  4 =>
    array (size=1)
      'sedloid' => string '7' (length=1)
  5 =>
    array (size=1)
      'sedloid' => string '8' (length=1) 


What am I doing wrong?

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
spopic
  • 92
  • 11

3 Answers3

0

This is your first item:

{"movieid":"1"}

This is what you do to it:

   $id_proj = $k['movieid'];

It has a movie id field, so that is fine.

   $name = $k['name'];

It doesn't have a name field, so you get an undefined index.


It looks like you are defining the data wrong is the first place and you actually want to send a single object with multiple fields and not an array of multiple objects, each of which has one field.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

Removing second argument in json_decode(). After that you will get an object of type stdClass (instead of an associative array) and you can access its properties directly, as I showed below.

if (isset($_POST['myData'])) {
    $json = json_decode($_POST['myData']); // delete 'true'
    echo "Output $json->movieid AND $json->name AND $json->phone ";
}

And your json must look like this:

{
  "movieid":"1",
  "name":"Random Name",
  "phone":"0601234567",
  "sedloid":"6",
  "sedloid":"7",
  "sedloid":"8"
}

To be more precise, the json should look like this:

{
  "movieid":"1",
  "name":"Random Name",
  "phone":"0601234567",
  "sedlo": {"id": "6", "id": "7", "id": "8"}
}
  • This is not working. `Notice: Trying to get property of non-object` – spopic Jun 25 '17 at 21:34
  • @Spopic hmm, are you use last version of my code? `"Output $json->movieid AND $json->name AND $json->phone "` I edited my answer a few minutes ago, before there was `"Output $json->id_proj AND $json->name AND $json->phone "` – Stanislav Belichenko Jun 25 '17 at 21:37
  • 1
    I think you can't declare twice the "sedloid" index in the same object – knrf Jun 25 '17 at 21:56
0

I think you're json structure is wrong, try to format it like this :

[{
    "movieid": "1",
    "name": "Random Name",
    "phone": "0601234567",
    "sedloid": ["6", "7", "8"]
  },
  {
    "movieid": "2",
    "name": "Random Name 2",
    "phone": "0123456",
    "sedloid": ["9", "8", "7"]
}] 

This site to check json structures may help : https://jsonlint.com/

knrf
  • 89
  • 6
  • I'm generating JSON with javaScript, but you're right, it's much easier to reformat JSON structure and then to process it in PHP. Thank you for your suggestion, – spopic Jun 25 '17 at 23:23