3

I currently have 3 arrays, $A, $B and $C that I need to merge into a singe array called $allArrays.

I know I can do this with array_merge

$allArrays = array_merge($A,$B, $C);

But sometimes not all 3 arrays will exist. Depending on if a user of this site I am working on activates an option that would create an array, I sometimes might only end up with 1 of the three, 2 of the three or maybe all 3.

So if user 1 actives an option that creates array $A and $B, while user 2 actives an option that creates array $A and $C, regardless one, both, or all three need to be merged to $allArrays.

How can I do this? Currently, if I don't active all three arrays, allArrays comes back blank and empty, 0.

I feel like I could easily achieve this with multiple if statements to see if all arrays exists but I feel like that's a bit excessive. I am new with PHP so looking into what to search for has been difficult.

Thanks for any help!

4

1 回答 1

6

在合并之前尝试简单的类型转换..

$A = $B = $C = null;
$allArrays = array_merge((array) $A, (array) $B, (array) $C);
于 2013-03-26T19:59:23.147 回答