非法字符串偏移“选项1” [英] Illegal string offset 'option 1'

查看:1122
本文介绍了非法字符串偏移“选项1”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我最初的code:

  $ camp_price =阵列(
   '选项1'=>阵列(
                    'ID'=> 6月30日至7月20日,
                    '周'=> 3,
                    week_price'=> 995),
   '选项2'=>阵列(
                    'ID'=> 6月30日至7月13日,
                    '周'=> 2,
                    week_price'=> 995)
  ); 的foreach($ camp_price为$名=> $值){
$ TOTAL_COST [$名称] = $值['周'] * $值['week_price'];
}

下面是HTML code。它完美地呼应了$ TOTAL_COST直到表单被提交。然后,它显示错误:


  

警告:非法字符串偏移选项1


我不明白:

 <选择名称=preferred_session>
  <期权价值=选项1<如果PHP($preferred_session =='选项1')echo'selected =入选'; ?>>
 ?< PHP的echo $ camp_price ['选项1'] ['身份证'],':',$ TOTAL_COST ['选项1']; ?>欧元
 < /选项>
 <期权价值=2<如果PHP($preferred_session =='选项2')回声选定=选择; ?>>
 ?< PHP的echo $ camp_price ['选项2'] ['身份证'],':',$ TOTAL_COST ['选项2']; ?>欧元
 < /选项>
< /选择>


解决方案

看起来像,当code触发错误运行,无论是 $ camp_price $ TOTAL_COST 不是一个数组而是一个字符串。

您可能希望只发生错误点之前打印这些变量的值,并查找任何地方,你可能无意中改变了他们。

Here is my initial code:

    $camp_price=array(
   'option 1' => array(
                    'id' => 'June 30 to July 20',
                    'weeks' => 3,
                    'week_price' => 995),
   'option 2' => array(
                    'id' => 'June 30 to July 13',
                    'weeks'=> 2,
                    'week_price' => 995)
  ); 

 foreach ($camp_price as $name=>$values) {
$total_cost[$name]=$values['weeks'] * $values['week_price'];
}

Here is the HTML code. It echo perfectly the $total_cost until the form gets submitted. It then show the error:

Warning: Illegal string offset 'option 1'

Which I don't understand:

  <select name="preferred_session">
  <option value="option 1" <?php if($preferred_session =='option 1') echo'selected="selected"'; ?>>
 <?php echo $camp_price['option 1']['id'],': ', $total_cost['option 1']; ?>euros
 </option>
 <option value="2" <?php if($preferred_session =='option 2') echo 'selected="selected"'; ?>>
 <?php echo $camp_price['option 2']['id'],': ', $total_cost['option 2']; ?>euros
 </option>
</select>

解决方案

Looks like, when the code triggering the error runs, either $camp_price or $total_cost is not an array but a string.

You may want to print the values of those variables just before the point where the error occurs, and look for any places where you might have unintentionally changed them.

这篇关于非法字符串偏移“选项1”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆