将新行插入多维数组php

时间:2012-08-20 11:03:55

标签: php

PHP:

如何将值插入多维数组中的特定行或在多维数组中插入空行?

自:

a ={ (0,1,2),
     (3,4,5),
     (6,7,8) }

成为

a ={ (0,1,2),
     (null,null,null),
     (3,4,5),
     (6,7,8) }

或null我想引入的值?

1 个答案:

答案 0 :(得分:3)

使用gerry-03 at 4warding dot com中的此功能应该可以:

function array_insert(&$input, $offset, $replacement){
    array_splice($input, $offset, 0, 0);
    $input[$offset] = $replacement;
}

演示:

$a = array(array(0, 1, 2), array(3, 4, 5), array(6, 7, 8));
array_insert($a, 1, array(9, 10, 11));

结果:

[0] => Array
    (
        [0] => 0
        [1] => 1
        [2] => 2
    )

[1] => Array
    (
        [0] => 9
        [1] => 10
        [2] => 11
    )

[2] => Array
    (
        [0] => 3
        [1] => 4
        [2] => 5
    )

[3] => Array
    (
        [0] => 6
        [1] => 7
        [2] => 8
    )