首先,我为我的英语道歉...
我必须在所有子阵列的第一个位置的多维数组中插入一个元素。
$docNum = "RT/2013-2014/0266";
$values =
Array
(
[0] => Array
(
[0] => 2014-08-07
[1] => Dl-Dis1
[2] => Discount
[3] => 7.500
[4] => 26.25
)
[1] => Array
(
[0] => 2014-08-07
[1] => Dl-Dis1
[2] => Discount
[3] => 7.500
[4] => 24.38
)
)
这两个是输入..
我给予组合的代码是
array_unshift($values, $docNum);
,我的输出是
Array
(
[0] => RT/2013-2014/0266
[1] => RT/2013-2014/0266
[2] => Array
(
[0] => 2014-08-07
[1] => Dl-Dis1
[2] => Discount
[3] => 7.500
[4] => 26.25
)
[3] => Array
(
[0] => 2014-08-07
[1] => Dl-Dis1
[2] => Discount
[3] => 7.500
[4] => 24.38
)
)
我想要的输出应该是
Array
(
[0] => Array
(
[0] => RT/2013-2014/0266
[1] => 2014-08-07
[2] => Dl-Dis1
[3] => Discount
[4] => 7.500
[5] => 26.25
)
[1] => Array
(
[0] => RT/2013-2014/0266
[1] => 2014-08-07
[2] => Dl-Dis1
[3] => Discount
[4] => 7.500
[5] => 24.38
)
)
我希望你能理解我在问什么......
我也已经看到[link] :( stackoverflow.com/questions/15398678/array-unshift-for-multidimensional-arrays)
请不要标记为重复..
谢谢你提前......
答案 0 :(得分:3)
你无法彻底解除它。这将在父数组上而不是子数组之前添加值。您可以先循环它,然后循环每个子数组,然后使用unshift。例如:
// generate the string with the value you'd unshift
$docNum = "RT/2013-2014/0266";
// build up your multidimensional array
$values = array(
array('2014-08-07', 'Dl-Dis1', 'Discount', 7.500, 26.25),
array('2014-08-07', 'Dl-Dis1', 'Discount', 7.500, 24.38),
);
// for every sub-array...
foreach($values as &$sub_array) { // & reference
// ... unshift your value
array_unshift($sub_array, $docNum);
}
echo '<pre>';
// check out the result
print_r($values);
输出如下:
Array
(
[0] => Array
(
[0] => RT/2013-2014/0266
[1] => 2014-08-07
[2] => Dl-Dis1
[3] => Discount
[4] => 7.5
[5] => 26.25
)
[1] => Array
(
[0] => RT/2013-2014/0266
[1] => 2014-08-07
[2] => Dl-Dis1
[3] => Discount
[4] => 7.5
[5] => 24.38
)
)