note 98494 added to function.array-merge

From: Date: Fri, 18 Jun 2010 15:09:29 +0000
Subject: note 98494 added to function.array-merge
Groups: php.notes 
Request: Send a blank email to php-notes+get-170244@lists.php.net to get a copy of this message
In response to some array_interlace functions which have been posted here; I needed a function that operates on a single array and interlaces it, while slicing into chunks, so here it is. $array = single array passed to the function (for example an array of objects from a DB query) $chunks = number of chunks to be sliced to (for example 3 columns) $preserveKeys = self explainatory returns an arrays of interlaced chunks function array_chunks_interlace( $array = array(), $chunks = 3, $preserveKeys = FALSE ) { if( empty($array) || count($array) == 1 || $chunks <= 1 ) return $array; $result = array(); $counter = 0; for( $i=0;$i<count($array);$i++ ) { for( $j=0;$j<$chunks;$j++ ) { if( isset($array[$counter+$j]) ) { if( $preserveKeys === TRUE ) ${'chunk'.($j+1)}[$counter+$j] = $array[$counter+$j]; else ${'chunk'.($j+1)}[] = $array[$counter+$j]; } } $counter += $chunks; } $result = array(); for( $c=0;$c<$chunks;$c++ ) if( isset(${'chunk'.($c+1)}) ) $result[$c] = ${'chunk'.($c+1)}; return $result; } ---- Server IP: 69.147.83.197 Probable Submitter: 77.254.93.178 ---- Manual Page -- http://www.php.net/manual/en/function.array-merge.php Edit -- https://master.php.net/note/edit/98494 Del: integrated -- https://master.php.net/note/delete/98494/integrated Del: useless -- https://master.php.net/note/delete/98494/useless Del: bad code -- https://master.php.net/note/delete/98494/bad+code Del: spam -- https://master.php.net/note/delete/98494/spam Del: non-english -- https://master.php.net/note/delete/98494/non-english Del: in docs -- https://master.php.net/note/delete/98494/in+docs Del: other reasons-- https://master.php.net/note/delete/98494 Reject -- https://master.php.net/note/reject/98494 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#170244) next »