1. 程式人生 > >PHP array_pad函式用法

PHP array_pad函式用法

<?php
//填充陣列到制定長度
$array = [1,2,3,4];
$array1 = [
    [1],[2],[3],[4]
];

print_r(array_pad($array,6, 'abc'));
//執行結果:Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => abc [5] => abc )

//如果 size 引數小於原始陣列的長度,該函式不會刪除任何元素
print_r(array_pad($array,3, 'abc'));
//執行結果:Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )
//如果您將 size 引數設定為負數切絕對值大於原始陣列長度,該函式會在原始陣列之前插入新的元素 //如果您將 size 引數設定為負數切絕對值不大於原始陣列長度,保持不變 print_r(array_pad($array,-6, 'abc')); //執行結果:Array ( [0] => abc [1] => abc [2] => 1 [3] => 2 [4] => 3 [5] => 4 ) //二維陣列和一維陣列類似 print_r(array_pad($array1,6, 'abc')); //Array ( [0] => Array ( [0] => 1 ) [1] => Array ( [0] => 2 ) [2] => Array ( [0] => 3 ) [3] => Array ( [0] => 4 ) [4] => abc [5] => abc )