note 109797 added to ref.array

From: Date: Tue, 21 Aug 2012 18:06:42 +0000
Subject: note 109797 added to ref.array
Groups: php.notes 
Request: Send a blank email to php-notes+get-190161@lists.php.net to get a copy of this message
Fill an array with items from another array, wrapping <?php function array_window(array $array, $window = 1) { //return array of size $window filled with elements from $array. // if count($array) is smaller than $window, // elements will be repeated in the returned array $count = count($array); if ($count >= $window) { $ret = array_slice($array, 0, $window); } else { //wraparound $ret = array(); for ($i = 0; $i < $window; ++$i) { $crsr = $i; if ($crsr >= count($array)) { $crsr = $crsr - count($array); } $ret[] = $array[$crsr]; } } return $ret; } ?> Usage: <?php $test = array('one', 'two', 'three', 'four', 'five', 'six'); print_r(array_window($test, 4)); ?> will print Array ( [0] => one [1] => two [2] => three [3] => four ) <?php $test = array('one', 'two', 'three', 'four', 'five', 'six'); print_r(array_window($test, 9)); ?> will print Array ( [0] => one [1] => two [2] => three [3] => four [4] => five [5] => six [6] => one [7] => two [8] => three ) ---- Server IP: 69.147.83.197 Probable Submitter: 24.89.139.58 ---- Manual Page -- http://www.php.net/manual/en/ref.array.php Edit -- https://master.php.net/note/edit/109797 Del: integrated -- https://master.php.net/note/delete/109797/integrated Del: useless -- https://master.php.net/note/delete/109797/useless Del: bad code -- https://master.php.net/note/delete/109797/bad+code Del: spam -- https://master.php.net/note/delete/109797/spam Del: non-english -- https://master.php.net/note/delete/109797/non-english Del: in docs -- https://master.php.net/note/delete/109797/in+docs Del: other reasons-- https://master.php.net/note/delete/109797 Reject -- https://master.php.net/note/reject/109797 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#190161) next »