php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #35093 foreach nullifies index of an indexed array
Submitted: 2005-11-03 21:10 UTC Modified: 2005-11-04 10:07 UTC
From: tony at marston-home dot demon dot co dot uk Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 4.4.1 OS: Windows XP
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 this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: tony at marston-home dot demon dot co dot uk
New email:
PHP Version: OS:

 

 [2005-11-03 21:10 UTC] tony at marston-home dot demon dot co dot uk
Description:
------------
When I use foreach($array as $key => $value) on an indexed array, afterwards the index value, when viewed with key($array) is null instead of an integer. This means that when my code tests the array to see if it is either indexed or associative it is given the wrong answer.

Reproduce code:
---------------
<?php
$array[] = 'string of data';

if (is_int(key($array))) {
	echo "array is indexed\n";
} else {
	echo "array is associativ\/n";
} // if

foreach ($array as $key => $value) {
    if ($key === key($array)) {
    	echo "they are the same\n";
    } else {
        echo "they are NOT the same\n";
    } // if
} // foreach

if (is_int(key($array))) {
	echo "array is indexed\n";
} else {
	echo "array is associative\n";
} // if
?>

Expected result:
----------------
The output 'array is indexed' should be produced both before and after the foreach() statement.

Actual result:
--------------
The key of an indexed array, when viewed with key($array), is not being reported as an integer.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-11-03 21:11 UTC] sniper@php.net
Please do not submit the same bug more than once. An existing
bug report already describes this very problem. Even if you feel
that your issue is somewhat different, the resolution is likely
to be the same. 

Thank you for your interest in PHP.


 [2005-11-03 21:25 UTC] tony at marston-home dot demon dot co dot uk
Duplicate of what bug?

I searched the bug database before submitting this, and none of the descriptions were anywhere near what I was experiencing. How am I supposed to know that the source of the bug is the same if the descriptions are different?

Your response is uninformative, and you really should introduce the status of DUPLICATE instead of BOGUS.
 [2005-11-03 21:31 UTC] derick@php.net
1. policy is to mark duplicates bogus (you should search better)
2. foreach is not the problem (key() is)
3. see bug #35084 (yes, this is the same)
 [2005-11-04 10:07 UTC] tony at marston-home dot demon dot co dot uk
Bug 35084 reports a problem with current() and next() whereas my problem happens with key(). How am I supposed to know they are the same?

The term BOGUS means 'this is not a bug, you idiot' whereas DUPLICATE means 'this has already been reported before'. Two different meanings require two different headings otherwise it could lead people to believe that all those bugs marked BOGUS are not bugs at all but down to incompetent programming. There is a difference, you know.

If your bug reporting software can't handle it then I suggest you update it immediately.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 30 22:01:31 2024 UTC