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.

Was it helpful?

Solution 3

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
     )
 )

OTHER TIPS

Best thing i came up with is this:

for($i=0; $i<5; $i++){
        $t = $i;
        $t1 = $t+3;
        $te= $i+5;
        $ti = $i+10;
        $a[] = array(
            'date' => $t,
            'bannana' => $te,
            'tea' => $ti,
        );

        $b[] = array(
            'date' => $t1,
            'lemon' => $te,
        );

        $ar1[]=$a;
        $ar2[]=$b;
    }



    foreach($a as $key=>$value){
        foreach($value as $k=>$v){
            $ar[$value['date']][$k]=$v;
        }
    }

    foreach($b as $key=>$value){
        foreach($value as $k=>$v){
            $ar[$value['date']][$k]=$v;
        }
    }

    echo var_dump($ar);

Where $a and $b are the 2 arrays and the field they are ordered by is 'date'. I looked in stack overflow but couldn't quite get another solution for this exact case.

this one produces the following var_dump():

array(8) { 
     [0]=> array(3) { 
          ["date"]=> int(0) 
          ["bannana"]=> int(5) 
          ["tea"]=> int(10) 
     }
     [1]=> array(3) { 
          ["date"]=> int(1) 
          ["bannana"]=> int(6) 
          ["tea"]=> int(11) 
     } 
     [2]=> array(3) { 
          ["date"]=> int(2) 
          ["bannana"]=> int(7) 
          ["tea"]=> int(12) 
     } 
     [3]=> array(4) { 
          ["date"]=> int(3) 
          ["bannana"]=> int(8) 
          ["tea"]=> int(13) 
          ["lemon"]=> int(5) 
     } 
     [4]=> array(4) { 
          ["date"]=> int(4) 
          ["bannana"]=> int(9) 
          ["tea"]=> int(14) 
          ["lemon"]=> int(6) 
     } 
     [5]=> array(2) { 
          ["date"]=> int(5) 
          ["lemon"]=> int(7) 
     } 
     [6]=> array(2) {
          ["date"]=> int(6) 
          ["lemon"]=> int(8) 
     } 
     [7]=> array(2) { 
          ["date"]=> int(7) 
          ["lemon"]=> int(9) 
     }
} 

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top