1

PHP have all sort of built in array functions. So I'm wondering if there is a function that is equivalent to the foreach loop in this code.

$args = array('var1', 'var2');
$args2 = array('var3', 'var4', 'var5', 'var6');

foreach ($args2 as $arg) {
    $args[] = $arg;
}

4 Answers 4

4

Yes, array_merge().

array array_merge ( array $array1 [, array $... ] )

Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.

If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.

Values in the input array with numeric keys will be renumbered with incrementing keys starting from zero in the result array.

Sign up to request clarification or add additional context in comments.

Comments

0

You should try the array_merge function

Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.

Comments

0

With array_merge function. But note that numeric keys will be renumbered!

Or you can use union operator: *if keys will be set

$array1 = array('key1' => 'zero_a', 'key2' => 'two_a', 'key3' => 'three_a');
$array2 = array('key4' => 'one_b', 'key5' => 'three_b', 'key6' => 'four_b');
$result = $array1 + $array2;

manual

1 Comment

Union operator won't work for example in op, array_merge is the way to go
0

array_merge function is used for merging multiple arrays.

http://php.net/manual/en/function.array-merge.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.