Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding arrays to multi-dimensional array within loop

Tags:

I am attempting to generate a multi-dimensional array with each sub array representing a row I want to insert into my DB. The reason for this is so I can use CodeIgniters batch_insert function to add each row to the DB.

I am attempting to create each sub array within a loop and insert it into a multidimensional array. Google suggested using array_merge, but after using 'print_r' on the multidimensional array with the code below, only the last sub-array is being displayed.

Here is my code:

$allplayerdata = array(); //M-D container array
for ($i = 1; $i <= 11; $i++)
{
    $playerdata = array(
                        'player_id' => $this->input->post('player' . $i),
                        'goals' => $this->input->post('playergoals' . $i),
                        'player_num' => $i,
                        'fixture_id' => $this->input->post('fixture_id')
                    );

    //Merge each player row into same array to allow for batch insert
    $allplayerdata = array_merge($allplayerdata, $playerdata);
}
print_r($allplayerdata);

Can anyone spot where I'm going wrong? Help is appreciated!

like image 335
Leopold Stotch Avatar asked Jul 31 '11 20:07

Leopold Stotch


People also ask

How do you append a multidimensional array in python?

You simply assign to a (new) variable. If you want a multidimensional array, simply add a new array as an array element. Shouldn't this be arr. append(...) instead of arr[0] = ... , to avoid IndexError: list assignment index out of range ?

How do you merge multidimensional arrays?

The array_merge_recursive() function merges one or more arrays into one array. The difference between this function and the array_merge() function is when two or more array elements have the same key. Instead of override the keys, the array_merge_recursive() function makes the value as an array.

Can we use for each loop for multidimensional array in Java?

To loop over two dimensional array in Java you can use two for loops. Each loop uses an index. Index of outer for loop refers to the rows, and inner loop refers to the columns. You can then get each element from the array using the combination of row and column indexes.


2 Answers

This is because array_merge is not the right operation for this situation. Since all the $playerdata arrays have the same keys, the values are overridden.


You want to use array_push to append to an array. This way you will get an array of $playerdata arrays.

array_push($allplayerdata, $playerdata);

Which is equivalent to adding an element with the square bracket syntax

$allplayerdata[] = $playerdata;

  • array_merge - Merge one or more arrays
  • array_push - Push one or more elements onto the end of array
  • Creating/modifying with square bracket syntax
like image 200
Mark Biesheuvel Avatar answered Oct 26 '22 13:10

Mark Biesheuvel


This will add the second array to the first array: A merge is something different.

$allplayerdata[] = $playerdata;
like image 23
JK. Avatar answered Oct 26 '22 13:10

JK.