php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #51797 valid arguments for foreach
Submitted: 2010-05-11 18:25 UTC Modified: 2010-05-12 08:14 UTC
From: rc at opelgt dot org Assigned: mike (profile)
Status: Not a bug Package: *General Issues
PHP Version: 5.2.13 OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: rc at opelgt dot org
New email:
PHP Version: OS:

 

 [2010-05-11 18:25 UTC] rc at opelgt dot org
Description:
------------
When I give an array to loop through the name of the array and if the array given 
is with a key, in this case $i, should make no difference.

PHP4 didnt make a warning, PHP5 instead does.

Test script:
---------------
foreach($array[$i] as $key => $val) results in an warning message.




Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-05-11 22:39 UTC] degeberg@php.net
-Status: Open +Status: Feedback
 [2010-05-11 22:39 UTC] degeberg@php.net
The following script works fine for me:

<?php
$array = array(
	array('foo','bar'),
	array('test','hello'),
);

$i = 0;
foreach ($array[$i] as $key => $val) {
	echo $key . $val;
}
?>

You'll have to provide a complete script that gives unexpected/incorrect 
warnings.
 [2010-05-11 22:51 UTC] dtajchreber@php.net
-Status: Feedback +Status: Open
 [2010-05-11 22:51 UTC] dtajchreber@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

The syntax you provided works just fine and doesn't emit a warning if $array[$i] is an array... for example:

$array[2] = range(5, 10); 
$i = 2; 
foreach($array[$i] as $key => $val) echo $key . '->' . $val . PHP_EOL;
 [2010-05-12 08:13 UTC] mike@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: mike
 [2010-05-12 08:14 UTC] mike@php.net
-Status: Closed +Status: Bogus
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 17 00:01:31 2025 UTC