php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #2956 array_walk not working as it did in php 3.0?
Submitted: 1999-12-10 20:54 UTC Modified: 1999-12-10 23:12 UTC
From: fvkuipers at home dot com Assigned:
Status: Closed Package: Misbehaving function
PHP Version: 4.0 Beta 3 OS: Linux (Red Hat 6)
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: fvkuipers at home dot com
New email:
PHP Version: OS:

 

 [1999-12-10 20:54 UTC] fvkuipers at home dot com
This example is taken from the php3 documentation, and works in php3:

$fruits = array("d"=>"lemon","a"=>"orange","b"=>"banana","c"=>"apple");

function test_alter( &$item1 ) {
   $item1 = 'bogus';
}

function test_print( $item2 ) {
   echo "$item2<br>\n";
}

array_walk( $fruits, 'test_print' );
array_walk( $fruits, 'test_alter' );
array_walk( $fruits, 'test_print' );

When I run this, I get the following and NOTHING else:

lemon<br>
orange<br>
banana<br>
apple<br>

It seems that the array is traversed the first time, but not the next two times... perhaps it needs to restore the array pointer??  See the modification to this code:

// The functions 'test_print' and 'test_alter' are as defined above...
// [snip]

array_walk( $fruits, 'test_print' );
reset($fruits);
array_walk( $fruits, 'test_alter' );
reset($fruits);
array_walk( $fruits, 'test_print' );

This produces the expected output of:

lemon<br>
orange<br>
banana<br>
apple<br>
bogus<br>
bogus<br>
bogus<br>
bogus<br>

This is the result of the first file I've worked on under php4... so there may be more bugs on the way!  HTH.

Fred

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [1999-12-10 21:00 UTC] fvkuipers at home dot com
Actually, it also states in the php3 documentation for array_walk that: 

"func will actually be working with the elements of arr, so any changes made to those elements will actually be made in the array itself"

However this is not true in php4 yet... I had to put a & before the $item1 parameter of test_alter to get the changes back to the array.  This is also not similar to php3 behaviour.  Removing the '&'  will not return the changes to the array. 
Unfortunately, I realized this immediately after I clicked submit the first time...

HTH.

Fred  again!

 [1999-12-10 23:12 UTC] andrei at cvs dot php dot net
Not a bug - working as designed.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri May 10 02:01:30 2024 UTC