Home > Enterprise >  How to get array data in a single array
How to get array data in a single array

Time:01-27

Array ( [0] => 1 [1] => 2 [2] => 1 [3] => 23 [4] => 5 [5] => 2 ) Array ( [0] => 1 [1] => 1 [2] => 1 [3] => 2 [4] => 3 [5] => 2 )

So I have this array. I want the all data in a single array like this.How to achieve this.

Array ( [0] => 1 [1] => 2 [2] => 1 [3] => 23 [4] => 5 [5] => 2 [6] => 1 [7] => 1 [8] => 1 [9] => 2 [10] => 3 [11] => 2 )

CodePudding user response:

How about array_merge:

$array1 = [1, 2, 1, 23, 4, 2];
$array2 = [1, 1, 1, 2, 3, 2];

var_dump(array_merge($array1, $array2));

CodePudding user response:

You should juste combine those two array using the array_merge function like this

$array1 = [1,2,1,23,5,2];

$array2 = [1,1,1,2,3,2];

$result = array_merge($array1, $array2);

CodePudding user response:

I think you could use the array_merge function provided by PHP. For example, the code

$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);

will produce the following output:

[color] => green
[0] => 2
[1] => 4
[2] => a
[3] => b
[shape] => trapezoid
[4] => 4

If you don't want to overwrite any data, you can simply use the operator, For example, the code

$array1 = array(0 => 'zero_a', 2 => 'two_a', 3 => 'three_a');
$array2 = array(1 => 'one_b', 3 => 'three_b', 4 => 'four_b');
$result = $array1   $array2;
var_dump($result);

will produce:

  [0]=>
  string(6) "zero_a"
  [2]=>
  string(5) "two_a"
  [3]=>
  string(7) "three_a"
  [1]=>
  string(5) "one_b"
  [4]=>
  string(6) "four_b"

Source:

https://www.php.net/manual/en/function.array-merge.php

CodePudding user response:

you can use spread opertaor in javascript :

const array1 = [1,2,3,4,5];
const array2 = [6,7,8,9,10];

const array3 = [...array1,...arry2];
conole.log(array3);
//output : [1,2,3,4,5,6,7,8,9,10]

CodePudding user response:

$arr1 = [ 1, 2, 1, 23, 4, 2 ];
$arr2 = [ 1, 1, 1, 2, 3, 2 ];
$arr3 = [ 30, 31, 32 ];
$arrOfArrays = [
    [ 40, 41 ],
    [ 50, 51 ]
];

$result = [ ...$arr1, ...$arr2, ...$arr3, ...array_merge(...$arrOfArrays) ];

print_r($result);

This will print:

Array
(
    [0] => 10
    [1] => 11
    [2] => 20
    [3] => 21
    [4] => 22
    [5] => 30
    [6] => 31
    [7] => 32
    [8] => 40
    [9] => 41
    [10] => 50
    [11] => 51
    [12] => 52
)
  •  Tags:  
  • Related