简体   繁体   English

尝试使用解码的JSON中的值时出现PHP未定义索引错误

[英]I'm getting PHP undefined index error when I try to use values from decoded JSON

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: 这是我在PHP PAGE上发送的JSON:
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: 这是来自解码的JSON值的VAR_DUMP结果:

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? 我究竟做错了什么?

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. 它具有电影ID字段,所以很好。

  $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 . 似乎您在定义错误的数据是当务之急,实际上您想发送一个包含多个字段的对象,而不是发送多个对象的数组,每个对象都有一个field

Removing second argument in json_decode(). 删除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. 之后,您将获得stdClass类型的对象(而不是关联数组),并且可以直接访问其属性,如下所示。

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: 而且您的json必须看起来像这样:

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

To be more precise, the json should look like this: 更准确地说,json应该如下所示:

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

I think you're json structure is wrong, try to format it like this : 我认为您是json结构错误,请尝试将其格式化为:

[{
    "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/ 这个检查json结构的网站可能会有所帮助: https : //jsonlint.com/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我在php中遇到多维数组的未定义索引错误 - I'm getting undefined index error for multidimensional array in php 当我尝试从Ajax进行POST时PHP未定义的索引数据 - PHP Undefined index data when I try to POST from Ajax 当我从 php 的下拉列表中获取 select 时,我正在获取 null 值。 有时,它会显示诸如“未定义索引”之类的错误 - I am getting null values when I select from dropdown in php. And sometime, it shows up error like "undefined index" 在PHP中,我在第2行收到错误消息,其中显示未定义的索引ID-addmsg? - In PHP, I'm getting an error message on line 2, which says Undefined Index ID-addmsg? 当我尝试从android发布JSON时,Zend $ _POST ['param_name']说“ Undefined index” - Zend $_POST['param_name'] says “Undefined index” when i try to post my JSON from android 在我尝试从数据库中获取值时获取“未定义”值 Ajax json - Getting "undefined" value Ajax json while i try to get values from database 当我从php页面到索引页面获取json数据时,出现名为“ undefine”的错误 - Getting error named 'undefine' when i fetch json data from php page to index page 当 laravel 中的第一个索引数据非常大时,我如何使用解码的 json 数据 - How can i use the decoded json data when the first index data is very large in laravel 当我点击索引页面而不是错误消息时,我只是得到一个空白页面.PHP - when i hit index page instead of error messages I'm just getting a blank page.PHP PHP对象具有值,但是当我尝试在FOREACH()中使用它时,我得到的是NULL值 - PHP Object has values but when I try to use it in FOREACH() I am getting NULL value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM