将项目推送到二维数组

时间:2018-01-09 17:06:02

标签: php arrays

我有这个二维数组:

Array
(
    [0] => Array
        (
            [parousia] => 
            [proforikos] => 
            [test] => 
            [ergasias] => 
            [telikos] => 
        )

    [1] => Array
        (
            [parousia] => 
            [proforikos] => 
            [test] => 
            [ergasias] => 
            [telikos] => 
        )

    [2] => Array
        (
            [parousia] => 
            [proforikos] => 
            [test] => 
            [ergasias] => 
            [telikos] => 
        )

    [3] => Array
        (
            [parousia] => 
            [proforikos] => 
            [test] => 
            [ergasias] => 
            [telikos] => 
        )

    [4] => Array
        (
            [parousia] => 
            [proforikos] => 
            [test] => 
            [ergasias] => 
            [telikos] => 
        )

    [5] => Array
        (
            [parousia] => 
            [proforikos] => 
            [test] => 
            [ergasias] => 
            [telikos] => 
        )

    [6] => Array
        (
            [parousia] => 
            [proforikos] => 
            [test] => 
            [ergasias] => 
            [telikos] => 
        )

    [7] => Array
        (
            [parousia] => 
            [proforikos] => 
            [test] => 
            [ergasias] => 
            [telikos] => 
        )

    [8] => Array
        (
            [parousia] => 
            [proforikos] => 
            [test] => 
            [ergasias] => 
            [telikos] => 
        )

    [9] => Array
        (
            [parousia] => 
            [proforikos] => 
            [test] => 
            [ergasias] => 
            [telikos] => 
        )

    [10] => Array
        (
            [parousia] => 
            [proforikos] => 
            [test] => 
            [ergasias] => 
            [telikos] => 
        )

    [11] => Array
        (
            [parousia] => 
            [proforikos] => 
            [test] => 
            [ergasias] => 
            [telikos] => 
        )

    [12] => Array
        (
            [parousia] => 
            [proforikos] => 
            [test] => 
            [ergasias] => 
            [telikos] => 
        )

    [13] => Array
        (
            [parousia] => 
            [proforikos] => 
            [test] => 
            [ergasias] => 
            [telikos] => 
        )

    [14] => Array
        (
            [parousia] => 
            [proforikos] => 
            [test] => 
            [ergasias] => 
            [telikos] => 
        )

    [15] => Array
        (
            [parousia] => 
            [proforikos] => 
            [test] => 
            [ergasias] => 
            [telikos] => 
        )

)

for loop中,我想在数组的末尾添加更多null个项目。虽然我尝试了以下代码:

            for ($i = 0; $i < $diff; $i++) {
              $array = array();
              $array['parousia'] = null;
              $array['proforikos'] = null;
              $array['test'] = null;
              $array['ergasias'] = null;
              $array['telikos'] = null;
              $finalarray = array_push($thearray, $array);
            }

其中diff是项目的数量,我想在数组的末尾添加,而thearray是代码上方的大数组。

虽然它似乎合法且正确,但for loop并未在thearray的末尾添加项目。

任何想法或解决方法?

1 个答案:

答案 0 :(得分:6)

// create inserted item
$item = array_fill_keys(['parousia','proforikos','test','ergasias','telikos'],null);
// make array with length equal  $diff, filled with $item 
$items = array_fill(0, $diff, $item);
// add them to $array
$array = array_merge($array, $items);