note 85029 deleted from function.array-merge by salathe

From: Date: Mon, 06 Sep 2021 22:45:36 +0000
Subject: note 85029 deleted from function.array-merge by salathe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-216894@lists.php.net to get a copy of this message
Note Submitter: ron at ronfolio dot com ---- I needed a function similar to ian at fuzzygroove's array_interlace, but I need to pass more than two arrays. Here's my version, You can pass any number of arrays and it will interlace and key them properly. <?php function array_interlace() { $args = func_get_args(); $total = count($args); if($total < 2) { return FALSE; } $i = 0; $j = 0; $arr = array(); foreach($args as $arg) { foreach($arg as $v) { $arr[$j] = $v; $j += $total; } $i++; $j = $i; } ksort($arr); return array_values($arr); } ?> Example usage: <?php $a = array('a', 'b', 'c', 'd'); $b = array('e', 'f', 'g'); $c = array('h', 'i', 'j'); $d = array('k', 'l', 'm', 'n', 'o'); print_r(array_interlace($a, $b, $c, $d)); ?> result: Array ( [0] => a [1] => e [2] => h [3] => k [4] => b [5] => f [6] => i [7] => l [8] => c [9] => g [10] => j [11] => m [12] => d [13] => n [14] => o ) Let me know if you improve on it.

« previous php.notes (#216894) next »