简体   繁体   English

多维数组中的array_unshift插入所有数组中的第一个元素

[英]array_unshift in multidimensional array insert at first element in all arrays

first of all i say sorry for my english.. 首先,我对我的英语说抱歉..

i have to insert a element in a multidimensional array at the first position on all the sub-array.. 我必须在所有子数组的第一个位置的多维数组中插入一个元素。

$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
    )

 )

these two are input.. 这两个是输入..

code i give to combine is 我给组合的代码是

array_unshift($values, $docNum);

and the output i have is 我的输出是

 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
    )

)

the output i want should be 我想要的输出应该是

 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
    )

)

i think hope you understand what i am asking.. 我希望你明白我在问什么..

and also i already see this [link]:(stackoverflow.com/questions/15398678/array-unshift-for-multidimensional-arrays) 而且我已经看到了[link] :( stackoverflow.com/questions/15398678/array-unshift-for-multidimensional-arrays)

pls dont mark as duplicate.. 请不要标记为重复..

thank you advance.. 谢谢你提前..

You can't unshift it outright. 你无法直接取消它。 That will prepend the values on the parent array not the sub arrays. 这将在父数组上而不是子数组之前添加值。 You can loop it first, then each sub array, then you use unshift. 您可以先循环它,然后循环每个子数组,然后使用unshift。 Example: 例:

// 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);

Output is as follow: 输出如下:

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
    )
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM