Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: How to push to an array an other array, but without it creating a new value to the array

Tags:

arrays

php

I have 2 numeric arrays. One that starts with 0 and ends with 24 - and an other that starts with 25 and ends with 50.

I want to merge/push the one that starts with 25 to continue the first one. so I have a new array that starts with 0 and ends with 50.

I am currently using array_push which isn't serving my intentions, I get something like this:

How do I push an array:

                  0 => string 'US' (length=2)
                  1 => string 'AU' (length=2)
                  2 => string 'CA' (length=2)
                  3 => string 'CN' (length=2)
                  4 => string 'FR' (length=2)
                  5 => string 'DE' (length=2)
                  6 => string 'GB' (length=2)
                  7 => string 'IT' (length=2)
                  8 => string 'JP' (length=2)
                  9 => string 'KR' (length=2)
                  10 => string 'RU' (length=2)
                  11 => string 'DZ' (length=2)
                  12 => string 'AO' (length=2)
                  13 => string 'AR' (length=2)
                  14 => string 'AT' (length=2)
                  15 => string 'AZ' (length=2)
                  16 => string 'BB' (length=2)
                  17 => string 'BY' (length=2)
                  18 => string 'BE' (length=2)
                  19 => string 'BM' (length=2)
                  20 => string 'BR' (length=2)
                  21 => string 'BG' (length=2)
                  22 => string 'CL' (length=2)
                  23 => string 'CO' (length=2)
                  24 => string 'CR' (length=2)
                  25 => array
                       25 => string 'ZZ'
                       26 => string 'XX'
                       27 => string 'WW' 

ect'.

This is what i want it to look like:

                  0 => string 'US' (length=2)
                  1 => string 'AU' (length=2)
                  2 => string 'CA' (length=2)
                  3 => string 'CN' (length=2)
                  4 => string 'FR' (length=2)
                  5 => string 'DE' (length=2)
                  6 => string 'GB' (length=2)
                  7 => string 'IT' (length=2)
                  8 => string 'JP' (length=2)
                  9 => string 'KR' (length=2)
                  10 => string 'RU' (length=2)
                  11 => string 'DZ' (length=2)
                  12 => string 'AO' (length=2)
                  13 => string 'AR' (length=2)
                  14 => string 'AT' (length=2)
                  15 => string 'AZ' (length=2)
                  16 => string 'BB' (length=2)
                  17 => string 'BY' (length=2)
                  18 => string 'BE' (length=2)
                  19 => string 'BM' (length=2)
                  20 => string 'BR' (length=2)
                  21 => string 'BG' (length=2)
                  22 => string 'CL' (length=2)
                  23 => string 'CO' (length=2)
                  24 => string 'CR' (length=2)
                  25 => string 'ZZ' (length=2)
                  26 => string 'XX' (length=2)
                  27 => string 'WW' (length=2) ... ECT'. 
like image 871
Imnotapotato Avatar asked Feb 04 '26 19:02

Imnotapotato


1 Answers

Simply use:

$array = $arr1 + $arr2

or

array_merge($arr1, $arr2);
like image 134
jitendrapurohit Avatar answered Feb 06 '26 08:02

jitendrapurohit