PHP: Pushing Array to Array

Using PHP, how can I push items to an array at the first level without going two levels deep?

$emails = array(); 

$contact_email = '1@example.com   ';
$contact_email_additional = '2@example.com, 3@example.com   ,    4@example.com';

array_push($emails, trim($contact_email));
array_push($emails, array_map('trim', explode(',', $contact_email_additional)));

echo '<pre>';
print_r($emails);
echo '</pre>';

Open in new window

The problem with the code above is that it puts an array inside an array, instead of adding the items to the array.
Array
(
    [0] => 1@example.com
    [1] => Array
        (
            [0] => 2@example.com
            [1] => 3@example.com
            [2] => 4@example.com
        )

)

Open in new window

LVL 10
skijAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
Instead of doing an array_push - do an array_merge.

$emails = array_merge($emails, array_map('trim', explode(',', $contact_email_additional)));

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Julian HansenCommented:
Sample code below. Working sample here
<?php
$emails = array(); 

$contact_email = '1@example.com   ';
$contact_email_additional = '2@example.com, 3@example.com   ,    4@example.com';

array_push($emails, trim($contact_email));
$emails = array_merge($emails, array_map('trim', explode(',', $contact_email_additional)));

echo '<pre>';
print_r($emails);
echo '</pre>';

Open in new window

Ray PaseurCommented:
All of the PHP array functions are documented in the online man pages.  It's worth taking a moment to read the descriptions (you can read them all in less than five minutes).  You'll be pleasantly surprised at how many array functions exist and how richly supportive they are!
http://php.net/manual/en/ref.array.php
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.