[英]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.