添加阵列环内多维数组 [英] Adding arrays to multi-dimensional array within loop

查看:102
本文介绍了添加阵列环内多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图生成每个子阵列重新$ P $一个多维数组psenting一排我想插入我的数据库。这样做的原因是这样我就可以用codeIgniters batch_insert功能给每一行添加到数据库中。

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.

我试图在循环中创建的每个子阵列,并将其插入一个多维数组。谷歌建议使用array_merge,但使用'print_r的与下面的code中的多维数组后,正在显示只有最后一个子阵列。

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.

下面是我的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);

任何人都可以发现我要去哪里错了吗?帮助是AP preciated!

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

推荐答案

这是因为你不想合并的数组。由于具有相同的键,值被重写。即array_merge创建一个一维阵列,而不是两维

This is because you don't want to merge the arrays. Since the have the same keys, the values are overridden. I.e. array_merge creates a one dimensional array instead of a two dimensional

尝试:

$allplayerdata[] = $playerdata;

array_push ($allplayerdata, $playerdata);

PHP.net:array_push

这篇关于添加阵列环内多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆