1

I am working on 2 multi-dimensional arrays:

$array1 = array(
    0 => array(
        'items' => array(
            1 => array('79-' => 'abc','80-'=>'123'),
            2 => array('79-' => 'Mapping item1','80-'=>'123'),
            3 => array('79-' => 'abc','80-'=>'123')
        )
    ),
    1 => array(
        'items' => array(
            1 => array('79-' => 'Mapping item2','80-'=>'123'),
        )
    )
);

This is the second array:

$array2 = array(
    2 => array(
        "A" => 'Mapping item1',
        "B" => array(1 => 'product1', 2 => 'product2', 3 => 'product3')
    ),
    3 => array(
        "A" => 'Mapping item2',
        "B" => array(1 => 'product4', 2 => 'product5', 3 => 'product6')
    )
);

I am trying to map the 2 arrays by the key value 79-, and make changes to the array1. Here is my code.

foreach ($array1 as $key => $orders) {
    $items = $orders['items'];
    $itemIndex = 1;
    foreach ($items as $k => $item) {
        foreach ($array2 as $row) {
            if (strpos($item['79-'], $row['A']) !== false) {
                foreach ($row['B'] as $ite) {
                    items[ $itemIndex ]['79-'] = $ite;
                    $itemIndex++;
                }
            }
        }
        $itemIndex++;
    }

    $orders['items'] = $items;
    $array1[ $key ] = $orders;
}

I want to return an array as below but my code doesn't work.

$expectedArray = array(
    [0]=> array(
       ['items'] => array(
          [1]=>array('79-'=>'abc','80-'=>'123'),
          [2]=>array('79-'=>'product1','80-'=>'123'),
          [3]=>array('79-'=>'product2','80-'=>'123'),
          [4]=>array('79-'=>'product3','80-'=>'123'),
          [5]=>array('79-'=>'abc','80-'=>'123')
       )
    ),
    [1]=>array(
       ['items'] => array(
          [1]=>array('79-'=>'product4','80-'=>'123'),
          [2]=>array('79-'=>'product5','80-'=>'123'),
          [3]=>array('79-'=>'product6','80-'=>'123'),          
       )
    )
);

Anyone knows how to solve this problem, please help. I've been working on this for hours.

4
  • change [A] to ["A"].. idem B / items[ $itemIndex ]['79-'] to $items / 'product6 to 'product6' Commented Sep 19, 2017 at 3:08
  • I just edited the typing mistakes, but it is not the solution. Commented Sep 19, 2017 at 3:17
  • Can you post a expected result? I'm having problems understanding what needs to be done. Commented Sep 19, 2017 at 3:29
  • The expected result is the last array above. Commented Sep 19, 2017 at 3:30

1 Answer 1

1

Hope this one will be helpful. Here we are using foreach, array_column and array_map

Try this code snippet here

$column=array_column($array2,"B","A");
foreach ($array1 as $key => $itemsArray)
{
    $finalArray=array();//maintaining a final array
    foreach($itemsArray["items"] as $itemKey => $item)
    {
        if(isset($column[$item["79-"]]))//checking key in the column array
        {
            $result=array_map(function($value) use($item){
               unset($item["79-"]);
               return array("79-"=>$value)+$item;
            },$column[$item["79-"]]);
            $finalArray=$finalArray+$result;//appending array with + operator
        }
        else
        {
            $finalArray[]=$item;
        }
    }
    $array1[$key]["items"]=$finalArray;//overwriting final array to existing one
}
print_r($array1);
Sign up to request clarification or add additional context in comments.

6 Comments

As I checked, the $finalArray does not contain the value of $result?
@Tedxxxx Yes it contains, Can you check your expected output and my snippet?
Yes I checked, the final result doesn't contrain value in the $result array such as 'product1', 'product2', etc. It doesn't replace the old data.
Oh your code returns the correct result, but I am sorry that I had a mistake in the input array. Besides '-79', I also have another element '80-', but with your snip code, It only returns '-79'. I will accept your answer due to my mistake. But could you help me with the above one.
right now, the output result is like this: eval.in/863954. But I want to keep the value '80-' in all new element that has been added.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.