php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44006 next function is not returning correctly
Submitted: 2008-01-31 20:07 UTC Modified: 2008-02-01 11:47 UTC
From: crashrox at gmail dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.2.5 OS: SunOS 5.10 Generic_127
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: crashrox at gmail dot com
New email:
PHP Version: OS:

 

 [2008-01-31 20:07 UTC] crashrox at gmail dot com
Description:
------------
Next function is not returning a value when it should be. Tried code sample below on a linux and windows distro both running PHP 5.2.1 and received desired results. When tested on SunOS 5.10 and PHP 5.2.4 the undesired results appeared.

Reproduce code:
---------------
$array = array(
			'key1' => array(
				'sub1_key1',
				'sub1_key2',
				'sub1_key3',
				'sub1_key4',
			),
			
			'key2' => array(
				'sub2_key1',
				'sub2_key2',
				'sub2_key3',
				'sub2_key4',
			)
		);
		
		foreach($array as $key => $sub_array) {
			
			foreach($sub_array as $sub_val) {
				echo $sub_val;
				
				if(next($sub_array))
					echo ', ';
			}
			
			$next = next($array);
			if(is_array($next)) {
				echo '||||';
			}
		
			print_r($next);
		
		}

Expected result:
----------------
sub1_key1, sub1_key2, sub1_key3, sub1_key4||||Array
(
    [0] => sub2_key1
    [1] => sub2_key2
    [2] => sub2_key3
    [3] => sub2_key4
)
sub2_key1, sub2_key2, sub2_key3, sub2_key4

Actual result:
--------------
sub1_key1, sub1_key2, sub1_key3, sub1_key4sub2_key1, sub2_key2, sub2_key3, sub2_key4

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-02-01 11:47 UTC] johannes@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

Don\'t use foreach + next/current/...
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 12:01:30 2024 UTC