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?
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