1

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.

5 Answers 5

4

You can use this:

$arrayfull[]=$add;
Sign up to request clarification or add additional context in comments.

Comments

2
$fullArray[] = $add;

You can use it.

Comments

1

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));

Comments

0

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;

Comments

0

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;

1 Comment

Check your index.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.