1

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?

0

2 Answers 2

3

You just need to append to $myArray inside the loop:

$addToMyArray = array('one','two','three');
foreach($addToMyArray as $newElement) {
    $myArray[] = array(
        'foo' => $newElement,
        'bar' => 'foo',
        'aaa' => 'bbb',
    );
}
Sign up to request clarification or add additional context in comments.

Comments

2

Did you mean something similar to this?

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

    $addToMyArray = array('one','two','three');

    foreach($addToMyArray as $newElement) {
        $myArray[] = array(
            'foo' => $newElement,
            'bar' => 'foo',
            'aaa' => 'bbb',
        );
    }

   return $myArray;
}

or something like this one:

function compileArray($values) {
    myArray = array();
    foreach($values as $newElement) {
        $myArray[] = array(
            'foo' => $newElement,
            'bar' => 'foo',
            'aaa' => 'bbb',
        );
    }

   return $myArray;
}

$result = array_merge(show_array(),compileArray(array('one','two','three')));

or, adding further flexibility:

function createElementArray($value) {
    return array(
        'foo'=>$value,
        'bar'=>'foo',
        'aaa'=>'bbb'
    );
}

$result = array_merge(
    show_array(),
    array_map(createElementArray, array('one','two','three'))
);

In order to have the code of a single array encapsulated into a function easily interchangeable.

1 Comment

I was doing exactly the same thing and it didn't worked, but after copypasting your code it works! Amazing, thanks a lot!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.