2

I would like to create two very alike arrays. The second array only has one more item then the first one...

So what I'm trying to achieve is:

$array_one = ('one', 'two', 'three');
$array_two = ('one', 'two', 'three', 'four');

Is there a function to do so? I've looked at things like array_push, array_shift, but those aren't functions meant to do this kind of behavior I think.

Any build in function to "push" one item on a copy of an array?

2 Answers 2

9

Try with:

$array_two = array_merge($array_one, array('four'));

or

$array_two   = $array_one;
$array_two[] = 'four';

or

$array_two = $array_one;
array_push($array_two, 'four');

or

$array_two = $array_one + array('four');

or

$array_two  = $array_one;
$array_two += array('four');
Sign up to request clarification or add additional context in comments.

3 Comments

More then enough options I see! Thanks!
Ofc, but time wasn't there to accept it yet! But it's done :) Thanks again!
@Michiel what you want more in this answer, it is good as par your question.
7

You can try

print_r($array_one + $array_two);

This will just add extra value.

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.