Php Codeigniter阵列到字符串转换错误
我在发布更新批时收到此错误。我看过其他关于这个的帖子,但我的行参考是DB_active_rec.php文件,其他人在他们的MC中看到了它。该错误不会停止更新批处理的发布,它会返回每个$rb\u项目的错误。 控制器:Php Codeigniter阵列到字符串转换错误,php,codeigniter,Php,Codeigniter,我在发布更新批时收到此错误。我看过其他关于这个的帖子,但我的行参考是DB_active_rec.php文件,其他人在他们的MC中看到了它。该错误不会停止更新批处理的发布,它会返回每个$rb\u项目的错误。 控制器: public function update_rb(){ $rb_items = array(); $rb_id=$this->input->post('id', TRUE); $rb_brand=$this->inp
public function update_rb(){
$rb_items = array();
$rb_id=$this->input->post('id', TRUE);
$rb_brand=$this->input->post('brand', TRUE);
$rb_tasted=$this->input->post('tasted', TRUE);
$rb_rating=$this->input->post('rating', TRUE);
$rb_comment=$this->input->post('comment', TRUE);
$i=0;
foreach($rb_id as $id){
$rb_items[$i] = array(
'id'=>$id,
'brand'=>$rb_brand[$i],
'rating'=>$rb_rating[$i],
'comment'=>$rb_comment[$i]
);
if(empty($rb_tasted)){
$rb_items[$i]['tasted']=0;
} else if
(in_array($id,$rb_tasted)){
$rb_items[$i]['tasted']=1;
} else{
$rb_items[$i]['tasted']=0;
}
$i++;
}
$this->model->update_rb($rb_items);
}
型号:
public function update_rb($rb_items){
$this->rb_db->update_batch('rb_selection',$rb_items,'id');
}
视图:
/>
有没有人看到这个错误或者知道我的代码中缺少了什么?谢谢你的帮助
print\u r($rb\u items)
返回Array([0]=>Array([rb\u id]=>192[品牌]=>Napa Valley Soda Co[评级]=>r0[评论]=>c0[品尝]=>1)[1]=>Array([rb\u id]=>193[品牌]=>Naturale Brew[评级]=>r1[评论]=>c1[品尝]=>1)[2]=>Array([rb id]=>194品牌]=>Naturale 90[评级]=>r2[评论]=>c2[1]=>1]
看一看上面有三个品牌。不管出现什么错误,所有这些都是正确发布的。您的html标记名称在名称属性后面有[],因此您将在发布后以数组的形式获取值。这就是你得到这个错误的原因 只需从name属性中删除[],然后提交表单
//您在名称后使用[]有什么具体原因吗?您的html标记名称在名称属性后有[],因此您将在发布后以数组形式获取值。这就是你得到这个错误的原因 只需从name属性中删除[],然后提交表单
//在名称后面使用[]有什么具体原因吗?在迭代时,您希望视图中有一个增量值
$i
,并且:
<tr>
<input type="hidden" name="comments[<?php echo $i;?>][id]" value="<?php echo $row['id'];?>">
<input type="hidden" name="comments[<?php echo $i;?>][brand]" value="<?php echo $row['brand'];?>">
<td><?php echo "<p>".$row['brand']."</p>";?></td>
<td><input type="checkbox" <?php if($row['tasted'] == 1){echo "checked = checked";}?> name="comments[<?php echo $i;?>][tasted]" value="<?php echo $row['id'];?>" id="tasted"/></td>
<td><input type="text" name="comments[<?php echo $i;?>][rating]" <?php echo $row['rating'];?>/></td>
<td><textarea name='comments[<?php echo $i;?>][comment]' id='comment' cols="350"><?php echo $row['comment'];?></textarea></td>
</tr>
控制器中所需的一切:
$this->model->update_rb($this->input->post('comments'));
但不要忘记验证 您希望视图中包含的内容是在迭代时具有增量值
$i
,并且:
<tr>
<input type="hidden" name="comments[<?php echo $i;?>][id]" value="<?php echo $row['id'];?>">
<input type="hidden" name="comments[<?php echo $i;?>][brand]" value="<?php echo $row['brand'];?>">
<td><?php echo "<p>".$row['brand']."</p>";?></td>
<td><input type="checkbox" <?php if($row['tasted'] == 1){echo "checked = checked";}?> name="comments[<?php echo $i;?>][tasted]" value="<?php echo $row['id'];?>" id="tasted"/></td>
<td><input type="text" name="comments[<?php echo $i;?>][rating]" <?php echo $row['rating'];?>/></td>
<td><textarea name='comments[<?php echo $i;?>][comment]' id='comment' cols="350"><?php echo $row['comment'];?></textarea></td>
</tr>
控制器中所需的一切:
$this->model->update_rb($this->input->post('comments'));
但不要忘记验证 我的CI版本2.1.2存在问题。 在第1407行的DB_active_rec.php文件中 这:
$not[]=$k.-'.$v代码>
应该是:
$not[]=$k2.-'.$v2代码>
以下是正确的回答:
我的CI版本2.1.2存在问题。
在第1407行的DB_active_rec.php文件中
这:
$not[]=$k.-'.$v代码>
应该是:
$not[]=$k2.-'.$v2代码>
以下是正确的回答:
将数组传递给此函数,它将解决您的问题
function replaceArrayToString($arr = array()) {
$newArr = array();
foreach($arr as $key=>$value)
{
if (is_array($value))
{
unset($arr[$key]);
//Is it an empty array, make it a string
if (empty($value)) {
$newArr[$key] = '';
}
else {
$newArr[$key] = $this->replaceArrayToString($value);
}
}
else {
$newArr[$key] = $value;
}
}
return $newArr;
}
将数组传递给此函数,它将解决您的问题
function replaceArrayToString($arr = array()) {
$newArr = array();
foreach($arr as $key=>$value)
{
if (is_array($value))
{
unset($arr[$key]);
//Is it an empty array, make it a string
if (empty($value)) {
$newArr[$key] = '';
}
else {
$newArr[$key] = $this->replaceArrayToString($value);
}
}
else {
$newArr[$key] = $value;
}
}
return $newArr;
}
据我所知,没有[]就不会创建数组<代码>打印($rb\u项)
返回数组()<代码>打印($rb\U项目)
with[]返回数组([0]=>Array([id]=>223[品牌]=>Q&V[评分]=>1r[评论]=>1[品尝]=>1)[1]=>Array([id]=>224[品牌]=>Quick Check[评分]=>2r[评论]=>2[品尝]=>1]),当我检查两种产品时。据我所知,没有[[]没有创建数组<代码>打印($rb\u项)
返回数组()<代码>打印($rb\U项目)
带有[]返回数组([0]=>数组([id]=>223[品牌]=>Q&V[评分]=>1r[评论]=>1[品尝]=>1)[1]=>数组([id]=>224[品牌]=>快速检查[评分]=>2r[评论]=>2[品尝]=>1]),当我检查两种产品时。