0

I would like to add additional keys and values to an associative array by function. I try to simplify my desire to this very simple example.

For example, I have the following array:

$array = array (
    'key1' => array(
        'attribute1' => 'value1',
        'attribute2' => 'value2',
        'attribute3' => 'value3',
    ),
    'key2' => array(
        'attribute1' => 'value1',
        'attribute2' => 'value2',
        'attribute3' => 'value3',
    )
);

While creating the array, some elements should come from a function:

$array = array (
    'key1' => array(
        'attribute1' => 'value1',
        'attribute2' => 'value2',
        'attribute3' => 'value3',
    ),
    getArrayElements(),
    'key2' => array(
        'attribute1' => 'value1',
        'attribute2' => 'value2',
        'attribute3' => 'value3',
    )
);

function getArrayElements()
{
    $new_array = array (
        'additional_key_1'  => array(
            'attribute1' => 'value1',
            'attribute2' => 'value2',
            'attribute3' => 'value3',
        ),
        'additional_key_2'  => array(
            'attribute1' => 'value1',
            'attribute2' => 'value2',
            'attribute3' => 'value3',
        ),
    );
    return $new_array;
}

I get the following result:

Array
(
    [key1] => Array
        (
            [attribute1] => value1
            [attribute2] => value2
            [attribute3] => value3
        )
    [0] => Array
        (
            [additional_key_1] => Array
                (
                    [attribute1] => value1
                    [attribute2] => value2
                    [attribute3] => value3
                )

            [additional_key_2] => Array
                (
                    [attribute1] => value1
                    [attribute2] => value2
                    [attribute3] => value3
                )

        )
    [key2] => Array
        (
            [attribute1] => value1
            [attribute2] => value2
            [attribute3] => value3
        )
)

I would like to have this result

Array
(
    [key1] => Array
        (
            [attribute1] => value1
            [attribute2] => value2
            [attribute3] => value3
        )
    [additional_key_1] => Array
        (
            [attribute1] => value1
            [attribute2] => value2
            [attribute3] => value3
        )
    [additional_key_2] => Array
        (
            [attribute1] => value1
            [attribute2] => value2
            [attribute3] => value3
        )
    [key2] => Array
        (
            [attribute1] => value1
            [attribute2] => value2
            [attribute3] => value3
        )
)

Is this possible? How can I achieve this?

6
  • 1
    Three dots does it all. Commented Feb 2, 2023 at 2:23
  • Thank you very much! Is there a downside to it? In the following post nobody accepted it as answer: stackoverflow.com/questions/35548170/… Commented Feb 2, 2023 at 2:30
  • You can show your appreciation on the provided page. (You shouldn't write "thank you"s as comments.) Don't trust the answer scores. I often answer old questions, so my answers take a while before they bubble up to the top. You can help it get there. @Phantom Commented Feb 2, 2023 at 2:31
  • Prior to PHP7.4, the technique fails with Parse error: syntax error, unexpected '...' (T_ELLIPSIS), expecting ']' This is no longer concern because everyone should be programming on more modern, supported versions of PHP. @Phantom Commented Feb 2, 2023 at 2:41
  • Is there also a solution for PHP7.3? Thanks for the clarification @mickmackusa Commented Feb 2, 2023 at 2:54

1 Answer 1

-1

Use array_merge().

$array = array_merge(
    array (
        'key1' => array(
            'attribute1' => 'value1',
            'attribute2' => 'value2',
            'attribute3' => 'value3',
        ),
        'key2' => array(
            'attribute1' => 'value1',
            'attribute2' => 'value2',
            'attribute3' => 'value3',
        )
    ), 
    get_array_elements()
);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.