Ask Your Question
0

Is it not possible to combine 2 arrays in PHP using concatenation?

asked 2021-11-22 11:00:00 +0000

pufferfish gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2021-09-23 17:00:00 +0000

djk gravatar image

Yes, it is possible to combine 2 arrays in PHP using concatenation operator (.) or array_merge() function.

Using concatenation operator:

$array1 = array(1, 2, 3);
$array2 = array("a", "b", "c");
$combinedArray = $array1 . $array2;
print_r($combinedArray);

Output:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => a
    [4] => b
    [5] => c
)

Using array_merge() function:

$array1 = array(1, 2, 3);
$array2 = array("a", "b", "c");
$combinedArray = array_merge($array1, $array2);
print_r($combinedArray);

Output:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => a
    [4] => b
    [5] => c
)
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-11-22 11:00:00 +0000

Seen: 12 times

Last updated: Sep 23 '21