[英]How to json_decode using foreach loop?
I am trying to parse the form submitted hidden input text using a foreach loop.我正在尝试使用 foreach 循环解析表单提交的隐藏输入文本。
<input type="hidden" id="snippet_tags" name="snippet_tags[]" value="["88","92","96","98"]">
Get this using the following function使用以下 function 获取此信息
$snippet_tags = json_decode($_POST['snippet_tags'], true);
and parse the values using foreach loop并使用 foreach 循环解析值
foreach ($snippet_tags as $selectedOption){
$ins_snippet_tag_data = array(
'snippet_id' => $insertDataReturnLastId,
'tag_id' => $selectedOption,
'priority' => 1,
);
$this->Constant_model->insertDataReturnLastId('snippets_tags', $ins_snippet_tag_data);
}
Here the problem is values of tag_id is not saving in database这里的问题是 tag_id 的值没有保存在数据库中
You can't use the same quotes to delimit the value and the strings inside it.您不能使用相同的引号来分隔其中的值和字符串。 You need to use single quotes around the value.
您需要在值周围使用单引号。
<input type="hidden" id="snippet_tags" name="snippet_tags[]" value='["88","92","96","98"]'>
The way you wrote it, it's treated as if you'd written value="["
and the rest is ignored.你写它的方式,它被视为你写了
value="["
并且 rest 被忽略。
Also, since you have []
after the name, $_POST['snippet_tags']
will be an array, so you need to loop over it.此外,由于名称后面有
[]
, $_POST['snippet_tags']
将是一个数组,因此您需要循环它。
foreach ($_POST['snippet_tags'] as $json) {
$snippet_tags = json_decode($json, true);
foreach ($snippet_tags as $selectedOption){
$ins_snippet_tag_data = array(
'snippet_id' => $insertDataReturnLastId,
'tag_id' => $selectedOption,
'priority' => 1,
);
$this->Constant_model->insertDataReturnLastId('snippets_tags', $ins_snippet_tag_data);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.