Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating master array from multiple arrays

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.

like image 610
Purus Avatar asked Feb 05 '26 09:02

Purus


1 Answers

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) 
     }
} 
like image 137
Hristo Valkanov Avatar answered Feb 07 '26 01:02

Hristo Valkanov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!