4

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)

pls dont mark as duplicate..

thank you advance..

2
  • 1
    Traverse and use array_unshift on the sub-arrays. Really nothing special to do. Commented Aug 8, 2014 at 8:13
  • thsnks for your comment u give a route to my answer.. Commented Aug 8, 2014 at 9:35

1 Answer 1

3

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. 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
    )
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.