[英]Undefined index in php array, when index does in fact exist
Ok so I know what an undefined index means but I SWEAR i have this index in my array.好的,所以我知道未定义索引的含义,但我发誓我的数组中有这个索引。 Lets have some context:
让我们有一些上下文:
I have an array of objects in javascript, each object looks like so:我在 javascript 中有一个对象数组,每个 object 看起来像这样:
let obj = {
crop: arr['Crop'],
pests: arr['Pests'],
chemical,
product: arr['Product'],
footnote: arr['Footnote'],
rate: arr['Rate / ha'],
max_no: arr['Max No'],
hi: arr['HI'],
mrl,
pcs_no: arr['PCS No'],
supplier: arr['Supplier'],
useByDate: arr['Use By Date'],
footnote: arr['Footnote'],
comment: arr['Comment']
};
I then use JSON.stringify()
and send the array to a php handler, simple enough, yes.然后我使用
JSON.stringify()
并将数组发送到 php 处理程序,很简单,是的。 I then use json_decode($array, true)
and use a foreach loop on said array.然后我使用
json_decode($array, true)
并在所述数组上使用 foreach 循环。 The only 2 indexes that are "undefined" are supplier and pests.唯一“未定义”的两个指标是供应商和害虫。 Right now I have ignored all the logic and just echo back each element in the foreach loop and I can see the indexes in the response.
现在我已经忽略了所有的逻辑,只是回显了 foreach 循环中的每个元素,我可以看到响应中的索引。 So I am very confused.
所以我很困惑。 If I uncomment the logic then I get the undefined error again.
如果我取消注释逻辑,那么我会再次收到未定义的错误。
Here is the php code:这是 php 代码:
$results = array();
$pests = json_decode($_POST['stringedArray'], true);
foreach($pests as $pest) {
$newCode = randomString(16);
$chemCodes = array();
$mrlCodes = array();
$chems = $pest['chemical'];
$mrls = $pest['mrl'];
if(is_array($chems)) {
foreach($chems as $chem) {
if(!empty($chem)) {
$code = processChemical($connection, trim($chem));
array_push($chemCodes, $code);
}
}
} else {
$code = processChemical($connection, trim($chems));
array_push($chemCodes, $code);
}
if(is_array($mrls)) {
foreach($mrls as $mrl) {
if(!empty($mrl)) {
$code = processMrl($connection, trim($mrl));
array_push($mrlCodes, $code);
}
}
} else {
$code = processMrl($connection, trim($mrls));
array_push($mrlCodes, $code);
}
$cropCode = processCrop($connection, trim($pest['crop']));
$pestCode = processPest($connection, trim($pest['pests'])); //UNDEFINED INDEX 'pests'
$supplierCode = processCompany($connection, trim($pest['supplier'])); //UNDEFINED INDEX 'supplier'
$data = array(
"code" => $newCode,
"crop_code" => $cropCode,
"pests" => $pestCode,
"product" => (!empty($pest['product']) ? $pest['product'] : NULL),
"footnote" => (!empty($pest['footnote']) ? $pest['footnote'] : NULL),
"rate" => (!empty($pest['rate']) ? $pest['rate'] : NULL),
"max_no" => (!empty($pest['max_no']) ? $pest['max_no'] : NULL),
"hi" => (!empty($pest['hi']) ? $pest['hi'] : NULL),
"pcs_no" => (!empty($pest['pcs_no']) ? $pest['pcs_no'] : NULL),
"supplier_code" => $supplierCode,
"use_by_date" => (!empty($pest['useByDate']) ? $pest['useByDate'] : NULL),
"comment" => (!empty($pest['comment']) ? $pest['comment'] : NULL),
"status" => 'active'
);
$check = checkForPest($connection, $data);
foreach($chemCodes as $chemCode) {
addPestChemical($connection, $chemCode, $newCode);
}
foreach($mrlCodes as $mrlCode) {
addPestMrl($connection, $mrlCode, $newCode);
}
//$result = addPest($connection, $data);
array_push($results, $pest);
}
echo json_encode($results);
I fixed it by adding || null
我通过添加
|| null
来修复它|| null
to the end of each value in the object before it was stringified. || null
到 object 中每个值的末尾,然后再进行字符串化。 I guess if there is no value it just acts like the index doesn't exist.我猜如果没有价值,它就像索引不存在一样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.