我有这个二维数组:
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
的末尾添加项目。
任何想法或解决方法?
答案 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);