user773961
user773961

Reputation: 141

<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][]  1
myformdata[user][]  2
myformdata[user][]  3

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

what is the problem ?

Upvotes: 0

Views: 2978

Answers (4)

Jared Farrish
Jared Farrish

Reputation: 49208

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]);
}

Upvotes: 1

datasage
datasage

Reputation: 19573

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

Upvotes: 1

Explosion Pills
Explosion Pills

Reputation: 191749

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 []

Upvotes: 0

rid
rid

Reputation: 63502

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.

Upvotes: 4

Related Questions