1

In the following code $stu is declared an array however PHP reports invalid argument for foreach(). Why?




echo "<table align='center' border='1px'><tr><td>";
echo "<form action='".$_SERVER['PHP_SELF']."' method='POST'>";
$students=array("Jack","John","Ryan");
foreach ($students as $key=>$stu)
 {
echo "Please select a grade for $stu:";
echo "<select name='grade'>";
echo "<option>Grade A</option>";
echo "<option>Grade B</option>";
echo "<option>Grade C</option>";
echo "<option>Grade D</option>";
echo "<option>Grade E</option>";
echo "</select><br/>";
  }
for ($i=0;$i<count($students);$i++)
{
    echo "<input type='hidden' name='stu[]' value='$students[$i]'>";
}

foreach($stu as $arr_contents)
{
echo "$arr_contents";
}

echo "<input type='hidden' name='posted' value='true'>";
echo "<input type='submit' value='Enter'>";
echo "</form>";
echo "</tr></td></table>";
?>

4
  • Where is $stu declared as an array? You seem to use it in the first foreach, which is probably what sets it to a non-array. Commented Aug 11, 2011 at 17:34
  • If you look at for loop before foreach loop you will see that stu[] is declared an array and on each for loop iteration, $stu is populated with $students contents. Commented Aug 11, 2011 at 17:50
  • You do not access $stu in the for loop so there will be nothing filled in it. What do you want to achieve in the second foreach loop? Commented Aug 11, 2011 at 18:19
  • Are you sure you don't want to use $students again in the second loop? Nowhere do you actually define $stu as an array, and pretending otherwise is just confusing us Commented Aug 11, 2011 at 19:21

2 Answers 2

2

$stu is defined in scope of the first foreach, which is closed before it is called in its own foreach. At the end of the first foreach loop, it will contain the last used string value, 'Ryan'.

// $stu is only known inside this block
foreach ($students as $key=>$stu)
{
}

If you want to echo the contents of $stu you will have to do it inside the first foreach loop.

Sign up to request clarification or add additional context in comments.

1 Comment

$stu will still be defined, and contain the last value from the foreach loop, which'd be 'Ryan'. That's a string, not an array, so the foreach loop OP is complaining about is right... specified argument is not an array.
2

The variable $students is not declared as an associative array with value as an array. It should be something like:

$students = array( "Jack" => array( 'array', 'contents' ), "John" => array( 'other', 'content') );

2 Comments

Well, that is what is confusing me, the for loop before foreach, $stu is declared an array and is populated with $students array contents on each loop iteration.
@dkjain as said in comments to the question, there's no access to $stu in the for loop. Are you sure you reported the whole code?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.