uno
uno

Reputation: 867

Array Push Inside an array

I want to push another array inside array.

This is my array. I want to push another array on textTabs .

Array
(
    [recipients] => Array
        (
            [signers] => Array
                (
                    [0] => Array
                        (
                            [email] => [email protected]
                            [tabs] => Array
                                (
                                    [checkboxTabs] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [tabLabel] => sampleCheckbox
                                                )
                                        )
                                    [textTabs] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [tabLabel] => CompanyName
                                                    [value] => Qwerty
                                                )

                                            [1] => Array
                                                (
                                                    [tabLabel] => TradingName
                                                    [value] => Qwerty
                                                )

                                            [2] => Array
                                                (
                                                    [tabLabel] => ContactName
                                                    [value] => RM
                                                )

                                        )

                                )

                        )

                )

        )

)

This is my array that i want to add:

$array2 = array(
               "tabLabel"=>"MonthlyTotal",
                "value"=>$monthlytotal
          );

And this is my php code:

$data = array_push($array1['recipients']['signers']['0']['tabs']['textTabs'], $array2);

But I was not able to push it. Thank you for your help.

Upvotes: 0

Views: 69

Answers (2)

unclexo
unclexo

Reputation: 3941

If you do love a portable way to do add an array or any other values to any key, you may do it using recursive function. Let's see an example below:

<?php

$old_array = [
    'one' => 'value for one',
    'two' => 'value for two',
    'three' => [
        'four' => 'value for four',
        'five' => 'value for five',
        'six' => [
            'seven' => 'value for seven',
            'eight' => 'value for eight',
        ],
    ],

];

function addArrayToKey($array, $callback){
    $result = [];
    foreach($array as $key => $value){
        if(is_array($value)) {
            $result[$key] = addArrayToKey($value, $callback);
        } else {
            $result[$key] = $callback($key, $value);
        }
    }

    return $result;
}


function callback($key, $value) {
    if('seven' == $key) {
        return ['one', 'two', 'three' => ['four', 'five']];
    }

    return $value;
}

echo '<pre>';
print_r(addArrayToKey($old_array, 'callback')); 

Upvotes: 1

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72299

No need of assignment of array_push() to $data,as child-array pushed to initial array itself. Just do:

array_push($array1['recipients']['signers']['0']['tabs']['textTabs'], $array2);
print_r($array1);

Output:- https://3v4l.org/sntUP

I always prefer doing assignment like below:

$array1['recipients']['signers']['0']['tabs']['textTabs'][] = $array2;

Output:-https://3v4l.org/nHHUB

Upvotes: 1

Related Questions