note 6546 modified in control-structures.foreach by irc-html

From: Date: Sat, 19 Jan 2002 19:24:10 +0000
Subject: note 6546 modified in control-structures.foreach by irc-html
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-25242@lists.php.net to get a copy of this message
For things like sorting algorithms, this doesn't work: foreach ($array as $n) { foreach ($array as $n2) { ... } } It will do the ...'s only as much as elements in $array. Even 'cloning' the array doing $arrclone = $array; foreach ($array as $n) { foreach ($arrclone as $n2) { ... } } 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 :) --was-- 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/en/control-structures.foreach.php

« previous php.notes (#25242) next »