Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create two almost identical arrays

Tags:

arrays

php

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?

like image 642
Michiel Avatar asked May 07 '26 08:05

Michiel


2 Answers

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');
like image 67
hsz Avatar answered May 09 '26 22:05

hsz


You can try

print_r($array_one + $array_two);

This will just add extra value.

like image 25
Bhumi Shah Avatar answered May 09 '26 21:05

Bhumi Shah