ccdavies
ccdavies

Reputation: 1606

Adding an array to an array

How can I add the array in $add to my full array, so its number [3]:

$add = array('email' => '[email protected]', 'm_field_id_9' => 'Name2');

My array printed out:

  Array ( 
        [0] => Array ( 
            [email] => [email protected]
            [m_field_id_9] => Name 
        ) 
        [1] => Array ( 
            [email] => [email protected]
            [m_field_id_9] => Name
        ) 
        [2] => Array ( 
            [email] => [email protected]
            [m_field_id_9] => Name 
        )
    ) 

I have tried array_push(), but I can't get it to add the array.

Upvotes: 1

Views: 116

Answers (5)

Doanh
Doanh

Reputation: 56

$fullArray[] = $add;

You can use it.

Upvotes: 2

Poiz
Poiz

Reputation: 7617

Ideal in your Case is to simply use the Square Brackets and indicate the 3rd Slot @index Nr. 2 .... because you are dealing with a multi-dimensionally array here....

    $add        = array('email' => '[email protected]', 'm_field_id_9' => 'Name2');
    $mainArray  = [
        [
            'email'         => '[email protected]',
            'm_field_id_9'  => 'Name',
        ],
        [
            'email'         => '[email protected]',
            'm_field_id_9'  => 'Name',
        ],
        [
            'email'         => '[email protected]',
            'm_field_id_9'  => 'Name',
        ],
    ];
    // SIMPLY USE THE SQUARE BRACKETS HERE FOR THIS...
    // TO OVERWRITE THE INDEX LOCATION#3 (3RD ARRAY) GO:
    $mainArray[2]    = $add;
    // TO JUST APPEND TO THE END OF THE LAST ITEM THUS CREATING A 4TH SUB-ARRAY, GO:
    $mainArray[]     = $add;

Upvotes: 0

caryarit ferrer
caryarit ferrer

Reputation: 326

You can use this:

$arrayfull[]=$add;

Upvotes: 4

Progrock
Progrock

Reputation: 7485

Array push should have been fine:

<?php
$items =
[
    [ 
        'email' => '[email protected]',
        'name' => 'Foo' 
    ],
    [ 
        'email' => '[email protected]',
        'name' => 'Bar'
    ],
    [
        'email' => '[email protected]',
        'name' => 'Baz'
    ]
];

$item =
    [
        'email' => '[email protected]',
        'name' => 'qux'
    ];

array_push($items, $item);
var_export($items);

Output:

array (
0 => 
array (
    'email' => '[email protected]',
    'name' => 'Foo',
),
1 => 
array (
    'email' => '[email protected]',
    'name' => 'Bar',
),
2 => 
array (
    'email' => '[email protected]',
    'name' => 'Baz',
),
3 => 
array (
    'email' => '[email protected]',
    'name' => 'qux',
),
)

It's a useful function if you have more than one element to add.

However it's easier to just append to the array:

$items[] = $item;

Or simply:

$items[3] = $item;

Upvotes: 0

Aman Kumar
Aman Kumar

Reputation: 4547

You may use array_merge in PHP

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

<?php

$array = [
 array('email' => '[email protected]', 'm_field_id_9' => 'Name1'),
 array('email' => '[email protected]', 'm_field_id_9' => 'Name2')
];

$add = [array('email' => '[email protected]', 'm_field_id_9' => 'Name3')];

echo "<pre>";
print_r(array_merge($array, $add));

Upvotes: 1

Related Questions