-3

I want to need multiple array combaine in one array

I have array

array(
  0=> test 1
)
array(
  0=> test 2
)
array(
  0=> test 3
)

I need expected output

`array(
 0=>Test1
1=>Test2
2=>test3

)`
6
  • cakephp and laravel? And mysql? Don't spam tags Commented Nov 28, 2022 at 10:43
  • 1
    What have you got so far? Where did you fail? Commented Nov 28, 2022 at 10:44
  • Is there any specification as to why test1 and test2 have their first letter uppercased? Why not test3? Commented Nov 28, 2022 at 10:47
  • 2
    Use array_merge Commented Nov 28, 2022 at 10:48
  • Does this answer your question? PHP combining arrays Commented Dec 1, 2022 at 21:34

3 Answers 3

2

You can use the array_merge() for this. The array_merge() function merges one or more arrays into one array.If two or more array elements have the same key, the last one overrides the others.

Syntax:

array_merge(array ...$arrays): array

Example:

$a1=array("red","green");
$a2=array("blue","yellow");
print_r(array_merge($a1,$a2));

Result:

Array ( [0] => red [1] => green [2] => blue [3] => yellow )

You can check more here.

Sign up to request clarification or add additional context in comments.

Comments

2
$a = array('test_1');
$b = array('test_2');
$c = array('test_3');
print_r(array_merge($a,$b,$c));

O/P - Array ( [0] => test_1 [1] => test_2 [2] => test_3 )

Comments

1

Hope you are doing well and good. So, as per your requirement i found solution to get result.

$array1 = [0 => "Test 1"]; $array2 = [0 => "Test 2"]; $array3 = [0 => "Test 3"];

print_r(array_merge($array1,$array2,$array3));

In the above example you have to merge the n number of array with single array, so for that you need to use array function which is array_merge(array ...$array).

What is array_merge()?

The array_merge() function merges one or more arrays into one array.

Tip: You can assign one array to the function, or as many as you like.

Note: If two or more array elements have the same key, the last one overrides the others.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.