note 10053 modified in function.array-pad by vrana

From: Date: Tue, 17 Aug 2004 14:48:03 +0000
Subject: note 10053 modified in function.array-pad by vrana
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-80013@lists.php.net to get a copy of this message
This is useful when using next() and prev() function in a while loop to traverse an array. For example the following code will only output up to 8. <?php $test[] = "1"; $test[] = "2"; $test[] = "3"; $test[] = "4"; $test[] = "5"; $test[] = "6"; $test[] = "7"; $test[] = "8"; $test[] = "9"; $test[] = "10"; $test[] = " "; $test[] = " "; $test[] = " "; $count = count($test); while($i < $count) { $now = current($test); echo "<p>$now</p>"; next($test); next($test); next($test); prev($test); prev($test); prev($test); $i++; next($test); } ?> But if you use: $test = array_pad($test, 13, " "); you will get all of your output. --was-- This is useful when using next() and prev() function in a while loop to traverse an array. For example the following code will only output up to 8. $test[] = "1"; $test[] = "2"; $test[] = "3"; $test[] = "4"; $test[] = "5"; $test[] = "6"; $test[] = "7"; $test[] = "8"; $test[] = "9"; $test[] = "10"; $test[] = " "; $test[] = " "; $test[] = " "; $count = count($test); while($i < $count) { $now = current($test); echo "<p>$now</p>"; next($test); next($test); next($test); prev($test); prev($test); prev($test); $i++; next($test); } But if you use: $test = array_pad($test, 13, " "); you will get all of your output. http://php.net/manual/en/function.array-pad.php

« previous php.notes (#80013) next »