-3

I've two arrays array1 and array2 and I want to add all elements of array2 to the end of array1. array1 contains many items.

The keys are numeric and I don't want this syntax:

array1 = array1 + array2

or

array1 = SomeArrayFun(array1,array2)

As it takes away CPU times ( as array is created twice )

What I want is:

array1 . SomeAddFun(array2); // This will not create any new arrays

Is there any way to do it?

hakre
  • 193,403
  • 52
  • 435
  • 836
user774250
  • 161
  • 2
  • 14
  • 1
    `As it takes away CPU times ( as array is created twice )`. This is minuscule, and you won't notice any difference at all. – gen_Eric Dec 21 '11 at 17:40
  • It is for heavy traffic site with thousands of records in the array – user774250 Dec 21 '11 at 17:42
  • 1
    Take a look at this post benchmarking array functions: http://stackoverflow.com/a/2484455 – gen_Eric Dec 21 '11 at 17:46
  • Ok so it seems `array_splice` is a *tiny bit* faster than `array_merge`. http://ideone.com/qyxS3 – gen_Eric Dec 21 '11 at 17:55
  • If every programmer can optimize better then we can save power and in turn lot of CO2 emission :) – user774250 Dec 21 '11 at 17:55
  • @Rocket: Nice link, +1, and I can imagine that `array_splice` is not always faster. Optimization needs concrete metrics, not a general use-case. – hakre Dec 21 '11 at 17:56
  • @user774250: Please back up that broad statement with some facts. I doubt that this will save any CO2 emission factually, would be nice, but I think the truth is something else. – hakre Dec 21 '11 at 18:11

2 Answers2

5

If you'd like to append data to an existing array you should se array_splice.

With the proper arguments you'll be able to insert/append the contents of $array2 into $array1, as in the below example.

$array1 = array (1,2,3);
$array2 = array (4,5,6);

array_splice ($array1, count ($array1), 0, $array2);

print_r ($array1);

output:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
)
Filip Roséen - refp
  • 62,493
  • 20
  • 150
  • 196
  • 1
    @user774250 don't forget to mark the answer as accepted if you are satisfied with the answer. – Filip Roséen - refp Dec 21 '11 at 17:53
  • Well actually, even if you don't see it, but [`array_splice`](http://lxr.php.net/opengrok/xref/PHP_5_3/ext/standard/array.c#2067) is creating a third hashtable internally. I would consider this shifts things only a bit around and [`array_merge`](http://lxr.php.net/opengrok/xref/PHP_5_3/ext/standard/array.c#2397) is probably faster as it doesn't have such a complicate function logic. You actually need to test which of those two ones is faster in the concrete usage. – hakre Dec 21 '11 at 17:54
  • @hakre: Seems `array_splice` is *slightly* faster than `array_merge`. http://ideone.com/qyxS3 – gen_Eric Dec 21 '11 at 17:56
  • @hakre thanks, this is quite a bit of insight but array_merge is worst because it completely destroys and recreates array1 and I don't think Zend engine can optimize it anyway due to intervening assignment operator – user774250 Dec 21 '11 at 17:59
  • 2
    @Rocket Use bigger arrays in your testcase and you will notice that `array_splice` is a lot faster when working with a bigger set. – Filip Roséen - refp Dec 21 '11 at 18:01
  • Note: if the array to append is significantly smaller than the destination array, it's even faster (in comparison). – Filip Roséen - refp Dec 21 '11 at 18:06
  • @hakre array_splice is made for splicing and not for appending the content so it's internal algorithm is oriented towards splicing. Your point is justified. In that case I should go with array_merge. There is no better function. – user774250 Dec 21 '11 at 18:08
  • If the arrays are small, go with array_merge. If they are larger, better use array_splice. Like refp commented, that's what I know as well. Additionally array_merge allows multiple parameters as well, can make a difference, too. Choose wisely. – hakre Dec 21 '11 at 18:09
  • @hakre I thought I'd play around, and I found an even faster method, I'll post it together with a testcase, stay tuned. And you want to see this, it's not the most straight forward way to do it, but it's fast as fcuk! – Filip Roséen - refp Dec 21 '11 at 18:15
0

You might use ArrayObject with the append function:

$arrayobj = new ArrayObject(array('first','second','third'));
$arrayobj->append('fourth');

Result:

object(ArrayObject)#1 (5) {
  [0]=>
  string(5) "first"
  [1]=>
  string(6) "second"
  [2]=>
  string(5) "third"
  [3]=>
  string(6) "fourth"
}

Don't know for appending arrays though, as they seem to be appended as a "subarray" and not as part of the whole.

Docs: http://www.php.net/manual/en/arrayobject.append.php

Telmo Marques
  • 5,066
  • 1
  • 24
  • 34
  • Or use [`array_push`](http://php.net/manual/en/function.array-push.php) on "normal" Arrays. – gen_Eric Dec 21 '11 at 17:44
  • Pushing an array will add whole array with an index and not merge it's elements – user774250 Dec 21 '11 at 17:53
  • @user774250, I only noticed it afterwards, and that's why I added the text at the bottom of the answer. I left my answer as it's still a way to append content to an array and might be useful information to others. – Telmo Marques Dec 21 '11 at 17:56