0

I'd like to build on $arr1 with the special row from $arr2.

$arr1 = Array
(
    [0] => Array
        (
            [1111] => Array
                (
                    [itemid] => 4321
                    [itemcode] => item1
                    [avail] => 0
                )
            [2222] => Array
                (
                    [itemid] => 4321
                    [itemcode] => item2
                    [avail] => 20
                )
         )
    [1] => Array
        (
            [3333] => Array
                (
                    [itemid] => 1342
                    [itemcode] => item3
                    [avail] => 10
                )
         )
)

$arr2 = Array
(
    [1111] => Array
        (
            [itemid] => 4321
            [itemcode] => item1
            [special] => 10
        )

    [2222] => Array
        (
            [itemid] => 4321
            [itemcode] => item2
            [special] => 2
        )

    [3333] => Array
        (
            [itemid] => 1342
            [itemcode] => item3
            [special] => 1
        )
)

My desired result is like so:

$arr1 = Array
(
    [0] => Array
        (
            [1111] => Array
                (
                    [itemid] => 4321
                    [itemcode] => item1
                    [avail] => 0
                    [special] => 10
                )
            [2222] => Array
                (
                    [itemid] => 4321
                    [itemcode] => item2
                    [avail] => 20
                    [special] => 2
                )
         )
    [1] => Array
        (
            [3333] => Array
                (
                    [itemid] => 1342
                    [itemcode] => item3
                    [avail] => 10
                    [special] => 1
                )
         )
)

I understand how to do this with two mutlidimensional arrays but the additional array wrapping around $arr1 has me thinking I need to for loop but I don't want to make a 3rd new array, just take on the special value to $arr1.

I've tried variations of the following:

$arr1 = array_map(function($v) use($arr2){
     $v = (array)$v;
     $key = array_search(array_column($v["itemcode"]), array_column($arr2, 'itemcode'));
     $v["special"] = $arr2[$key]["special"];
     return $v;
}, $arr1);

Which fails to append the data to the special index even though the new index appears in $arr1(new).

Guidance is much appreciated! Maps are so cool and powerful but I cannot seem to grasp their awesomeness in its entirety.

2
  • What is $mini_data? Commented Sep 14, 2018 at 20:26
  • @Barmar a mistake, it's supposed to be $arr2. I've corrected it Commented Sep 14, 2018 at 20:28

2 Answers 2

3

Just use nested foreach loops. Use reference variables so you can modify the original array in place.

foreach ($arr1 as &$el1) {
    foreach ($el1 as $key => &$el2) {
        $el2['special'] = $arr2[$key]['special'];
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

It's so simple! Thank you so much, I keep defaulting to the harder more complex functions!
1

Another way to modify $arr1 without a reference:

    foreach($arr1 as $k => $v){
        foreach($v as $idx => $elem) {
            if(array_key_exists($idx, $arr2)) {
                $arr1[$k][$idx]['special'] = $arr2[$idx]['special'];
            }
        }
    }

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.