note 82871 added to function.array-merge

From: Date: Tue, 29 Apr 2008 23:51:43 +0000
Subject: note 82871 added to function.array-merge
Groups: php.notes 
Request: Send a blank email to php-notes+get-138922@lists.php.net to get a copy of this message
I needed a function to interlace two arrays ([a,b,c] + [d,e,f] = [a,d,b,e,c,f]) and came up with the following. This function works for arrays of varying lengths and keeps the keys sequenced properly. <?php $a = array ('a', 'b', 'c'); $b = array ('d', 'e', 'f', 'g', 'h', 'i'); $c = array_interlace ($a, $b); /* Array ( [0] => a [1] => d [2] => b [3] => e [4] => c [5] => f [6] => g [7] => h [8] => i ) */ function array_interlace ($a, $b) { $c = array(); $shorty = (count($a) > count($b)) ? $b : $a; $biggy = (count($a) > count($b)) ? $a : $b; $slen = count($shorty); $blen = count($biggy); for ($i = 0; $i < $slen; ++$i){ $c[$i * 2] = $a[$i]; $c[$i * 2 + 1] = $b[$i]; } for ($i = $slen; $i < $blen; ++$i) { $c[] = $biggy[$i]; } return $c; } ?> ---- Server IP: 69.147.83.197 Probable Submitter: 74.1.189.48 ---- Manual Page -- http://www.php.net/manual/en/function.array-merge.php Edit -- https://master.php.net/note/edit/82871 Del: integrated -- https://master.php.net/note/delete/82871/integrated Del: useless -- https://master.php.net/note/delete/82871/useless Del: bad code -- https://master.php.net/note/delete/82871/bad+code Del: spam -- https://master.php.net/note/delete/82871/spam Del: non-english -- https://master.php.net/note/delete/82871/non-english Del: in docs -- https://master.php.net/note/delete/82871/in+docs Del: other reasons-- https://master.php.net/note/delete/82871 Reject -- https://master.php.net/note/reject/82871 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#138922) next »