Question: I have 2 arrays(which could be more too) and they might have scattering data. 

```
Date    | Apple                       Date    | Banana  
-----------------                    -------------------
   1      |   5                         1     |   3
   4      |   5                         3     |   1
   5      |   5                         7     |   2

```

The array structure would be like as below.

```
array
(
   [0] => array
   (
      [date] => 4
      [apple] => 5
   )
   [1] => array
   (
       [date] => 5
       [apple] => 5
   )
)

```

What I need is to have a consolidated array having both the details. Example shown below. 

```
Date      Apple      Banana
-------------------------------    
 1          5          3
 3          0          1
 4          5          0
 5          5          0
 7          0          2

```

How best we can do this considering the fact that array can hold multiple values? Any hint will help me to solve this. Or please point me to correct link if its already discussed. I could not find anything during search.

Thanks in advance.

Answer: 
Based on @Hristo response, I modified the code and it worked the following way. By this way, I get the expected output.

Below is not the exact code that I used. I have modified to be in alignment with my example posted in the question.

Will this work well in a huge array?

```
    $finalArray = array();

    foreach ($apple as $key => $value) {
         $finalArray[$value['date']]['apple'] = $value['apple'];
    }

    foreach ($banana as $key => $value) {
         $finalArray[$value['date']]['banana'] = $value['banana'];
    }

    var_dump($finalArray);

```

This gives an output of 

```
 array
 (
     [1] => array
     (
        [apple] => 5
        [banana] => 3
     )
 )

```
Using for loops, you can iterate through the array that is to be added, find an existing 'Date' entry in the master table or create a new one that matches the current 'Date' value of the array being added. If the data of an entry in the master table requires a new attribute type (e.g. Apple, Banana) that it doesn't have, then add it to the data. I would also recommend that you use the 'Date' as the array key as well, it will be easier to quickly find matches this way:

```
foreach ($appleArray as $value) {
  $date = $value['date'];
  if (isset($masterArray[$date])) {
    // Goes here if the date exists in the master array already.
    // In this case, we just append to our data the fields
    // found in the $value.
    foreach ($value as $subKey => $subValue) {
      // Iterate through each value, skip our 'date' key, and append
      // each other item to our master array.
      if ($subKey != 'date') {
        $masterArray[$date][$subKey] = $subValue;
      }
    }
  } else {
    // Goes here if the date isn't in the master array yet.
    // In this case, create a new entry and just copy the data.
    $masterArray[$date] = $value;
  }
}

```

In this system, there will be fields missing when one date entry contains an apple but not a banana, etc. In these cases, you can either just assume those values are 0, or iterate through the entire list and add the 'banana' field manually.