0

I am having two arrays, in that i need to insert the each index of last key and value of another array keys, values in php. My sample arrays are given below. I am using codeigniter framework.

First array:

Array
(
    [0] => stdClass Object
        (
            [customer_name] => Cash
            [ordernumber] => 6452424
            [product_name] => Bacardi Rum
            [quantity] => 1
            [unit_price] => 25.00
            [inv_discount] => 0.00
            [salesman_id] => 25,27
        )

    [1] => stdClass Object
        (
            [customer_name] => Cash
            [ordernumber] => 6452424
            [product_name] => Baileys
            [quantity] => 1
            [unit_price] => 15.00
            [inv_discount] => 0.00
            [salesman_id] => 28,29
        )

)

Second array:

Array
(
    [0] => 140140,150150
    [1] => 151151,05180518
)

And i need the o/p :

Array
    (
        [0] => stdClass Object
            (
                [customer_name] => Cash
                [ordernumber] => 6452424
                [product_name] => Bacardi Rum
                [quantity] => 1
                [unit_price] => 25.00
                [inv_discount] => 0.00
                [salesman_id] => 25,27
                [salesman] => 140140,150150
            )

        [1] => stdClass Object
            (
                [customer_name] => Cash
                [ordernumber] => 6452424
                [product_name] => Baileys
                [quantity] => 1
                [unit_price] => 15.00
                [inv_discount] => 0.00
                [salesman_id] => 28,29
                [salesman] => 151151,05180518
            )

    )

Can any one help me, give some ideas to solve this.

4
  • 1
    Do a loop and use the key to identify element in the other array. Commented Aug 21, 2015 at 12:42
  • i did loop but i cant get exact solution. Again i'm checking and let you know Commented Aug 21, 2015 at 12:47
  • Then add it with your question. Commented Aug 21, 2015 at 12:48
  • i checked my loop was wrong. Now i got the solution. Thank you guys .... Commented Aug 21, 2015 at 12:54

3 Answers 3

3

In this case, you have an array of objects (stdClass type) and one other array only. Answering your question you just have to do the code as shown below.

foreach ($secondArray as $key => $value) {
    $firstArray[$key]->salesman = $value;
}

or

foreach ($firstArray as $key => $object) {
    $object->salesman = $firstArray[$key];
}
Sign up to request clarification or add additional context in comments.

1 Comment

@JaganAkash I'm happy to help you!
0
foreach($arrA as $key=>$val){
   $arrA[$key]['salesman'] = $arrB[$key];
}

3 Comments

he has 2 arrays, I have looped through 1st array and added in its each sub array an item from second array where both array's indexes are same
The reason for the comment above is your answer was flagged for review (and put in the low quality posts list) because on SO code only answers are discouraged. I reviewed your answer from the review queue and instead of voting to delete it I made my comment and skipped the vote using the skip button.
thank you, i will be adding some text along with code to avoid this in future
0

As long as both arrays will be same size, array_map will be suitable:

$resultArray = array_map(function ($rowA, $rowB) {
    $rowA->salesman = $rowB;
    return $rowA;
}, $firstArray, $secondArray);

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.