note 97108 deleted from function.array-combine by danbrown

From: Date: Thu, 01 Apr 2010 20:20:31 +0000
Subject: note 97108 deleted from function.array-combine by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-167631@lists.php.net to get a copy of this message
Note Submitter: bradentketih at dot dontspam dot gmail dot com ---- I needed a function that would take keys from one unequal array and combine them with the values of another. Real life application: Select 4 product types. Each product has a serial. There are 4 sets of products. <?php function array_combine2($arr1, $arr2) { $count1 = count($arr1); $count2 = count($arr2); $numofloops = $count2/$count1; $i = 0; while($i < $numofloops){ $arr3 = array_slice($arr2, $count1*$i, $count1); $arr4[] = array_combine($arr1,$arr3); $i++; } return $arr4; } ?> Input: Array ( [0] => SMART Board [1] => Projector [2] => Speakers [3] => Splitter ) , Array ( [0] => serial to smart board1 [1] => serial to projector 1 [2] => serial to speakers 1 [3] => serials to splitter 1 [4] => serials to smart board 2 [5] => serials to projector 2 [6] => serials to speakers 2 [7] => serials to splitter 2 ) Array ( [0] => Array ( [SMART Board] => serial to smart board1 [Projector] => serial to projector 1 [Speakers] => serial to speakers 1 [Splitter] => serials to splitter 1 ) [1] => Array ( [SMART Board] => serials to smart board 2 [Projector] => serials to projector 2 [Speakers] => serials to speakers 2 [Splitter] => serials to splitter 2 ) )

« previous php.notes (#167631) next »