1. My array
I have functon that returns an array of arrays:
function show_array() {
$myArray = array(
array(
'foo' => 'bar',
'bar' => 'foo',
'aaa' => 'bbb'),
array(
'foo' => 'bar2',
'bar' => 'foo',
'aaa' => 'bbb'),
array(
'foo' => 'bar3',
'bar' => 'foo',
'aaa' => 'bbb'),
array(
'foo' => 'bar4',
'bar' => 'foo',
'aaa' => 'bbb'),
//I want to add additional elements here using foreach
);
return $myArray;
}
2. Elements to add dynamically
As stated in the comment above I want to add some additional elements to $myArray based on foreach loop, here's a simple function that returns nothing, but shows what I want to insert there:
$addToMyArray = array('one','two','three');
foreach($addToMyArray as $newElement) {
array(
'foo' => $newElement,
'bar' => 'foo',
'aaa' => 'bbb',
);
}
3. Desired result
So in the end show_array() should return:
array(
array(
'foo' => 'bar',
'bar' => 'foo',
'aaa' => 'bbb'),
array(
'foo' => 'bar2',
'bar' => 'foo',
'aaa' => 'bbb'),
array(
'foo' => 'bar3',
'bar' => 'foo',
'aaa' => 'bbb'),
array(
'foo' => 'bar4',
'bar' => 'foo',
'aaa' => 'bbb'),
//added stuff
array(
'foo' => 'one,
'bar' => 'foo',
'aaa' => 'bbb'),
array(
'foo' => 'two,
'bar' => 'foo',
'aaa' => 'bbb'),
array(
'foo' => 'three,
'bar' => 'foo',
'aaa' => 'bbb'),
);
I was trying to return new options as $myArray[], do array_push on them and then array_merge, but nothing seems to work, I was also unable to place any loops within the $myArray array (what is obvious). But show_array() never returns generated elements.
How it should be done?