note 6546 modified in foreach by ronabop

From: Date: Fri, 18 Aug 2000 03:42:39 +0000
Subject: note 6546 modified in foreach by ronabop
Groups: php.notes 
Request: Send a blank email to php-notes+get-2292@lists.php.net to get a copy of this message
For things like sorting algorithms, this doesn't work: <pre> foreach ($array as $n) { foreach ($array as $n2) { ... } } </pre> It will do the ...'s only as much as elements in $array. Even 'cloning' the array doing <pre> $arrclone = $array; foreach ($array as $n) { foreach ($arrclone as $n2) { ... } } </pre> the problem is the assignment of the array. Saying $arrclone = $array makes $arrclone adapt the same array-pointer, apparently. What does work is to include within the first foreach-loop: $arrclone[$n] = $array[$n]; It's a bit tricky, but it works :) http://www.php.net/manual/control-structures.foreach.php

« previous php.notes (#2292) next »