开发者

<b>Fatal error</b>: Cannot use [] for reading

This code doesn't work > <b>Fatal error</b>: Cannot use [] for reading i

  <select style=" width:200px" class="mydds"  name="myformdata[user][]">

this is what is sent.

myformdata[user][开发者_Python百科]  1
myformdata[user][]  2
myformdata[user][]  3

  foreach($form['user'][] as $val){
            echo ($val);
    }

what is the problem ?


It's a syntax error. The correct syntax is:

foreach($form['user'] as $val)

The [] syntax is used for appending data to an array. For example:

$form['user'][] = 'test';

The above will add a new string to the $form['user'] array with the value of test.


foreach($form['user'] as $val){
    echo ($val);
}


Your syntax is wrong. Others are suggesting foreach(), I will suggest a for() loop in addition:

for ($i = 0; $i < count($form['user']); $i++){
    echo ($form['user'][$i]);
}


The result of the posted information (if I understand your explanation correctly) is:

$_REQUEST['myformdata']['user'] = array(1,2,3)

As the error states very clearly, you cannot use [] for reading. When you use foreach, the code is trying to read the value before as. It can't read with []. Instead try:

foreach ($_REQUEST['myformdata']['user'] as $val) { echo $val; }

Or if the information really is in $form['user'] just use that and skip the []

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜