note 43187 modified in function.array-merge by vrana

From: Date: Tue, 17 Aug 2004 13:58:41 +0000
Subject: note 43187 modified in function.array-merge by vrana
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-79975@lists.php.net to get a copy of this message
[Editor's Note: If the keys are truly numeric, array_merge will do the trick. But if they are strings, as the example below, you can use array_merge(array_values($array1), array_values($array2)); since it's more beautiful. -- victor@php.net] i recognized there is no function with that you can add two number-Arrays: Example: <?php // you have two arrays: array1 = array (['0'] =>"blahblupp", ['1'] => "bluppblah" ); array2 = array (['0'] =>"tirili", ['1'] => "tralala" ); // and want following as a result: result = array (['0'] =>"blahblupp", ['1'] => "bluppblah", ['2'] =>"tirili", ['3'] => "tralala" ); // following function does the addition: function array_add($array1, $array2) { $result = $array1; $h = sizeof($array1); for ($i = 0; $i < sizeof($array2); $i++) { $result[$h] = $array2[$i]; $h++; } return $result; } ?> just added because I didn't find a ready-to-use script here. --was-- [Editor's Note: If the keys are truly numeric, array_merge will do the trick. But if they are strings, as the example below, you can use array_merge(array_values($array1), array_values($array2)); since it's more beautiful. -- victor@php.net] i recognized there is no function with that you can add two number-Arrays: Example: you have two arrays: array1 = array (['0'] =>"blahblupp", ['1'] => "bluppblah" ); array2 = array (['0'] =>"tirili", ['1'] => "tralala" ); and want following as a result: result = array (['0'] =>"blahblupp", ['1'] => "bluppblah", ['2'] =>"tirili", ['3'] => "tralala" ) following function does the addition: function array_add($array1, $array2) { $result = $array1; $h = sizeof($array1); for ($i = 0; $i < sizeof($array2); $i++) { $result[$h] = $array2[$i]; $h++; } return $result; } just added because I didn't find a ready-to-use script here. http://php.net/manual/en/function.array-merge.php

« previous php.notes (#79975) next »