Merge 2 separate arrays into a single array

177 views Asked by At

I have getting array like:

Array ( [0] => 2020-11-21 [1] => 2020-11-22 [2] => 2020-11-23 ) 
Array ( [0] => 2020-10-11 [1] => 2020-10-12 [2] => 2020-10-13 [3] => 2020-10-14 [4] => 2020-10-15 )

Want to combine both arrays in a single array

expected result:

Array ( [0] => 2020-11-21 [1] => 2020-11-22 [2] => 2020-11-23 [3] => 2020-10-11 [4] => 2020-10-12 [5] => 2020-10-13 [6] => 2020-10-14 [7] => 2020-10-15 )

I have tried array_merge like:

public function method($format = 'Y-m-d') {
        $arrayObject = $this->context->value('event_date');
        $interval = new DateInterval('P1D');
        $realEnd = new DateTime($arrayObject['end']); 
        $realEnd->add($interval); 
        $period = new DatePeriod(new DateTime($arrayObject['start']), $interval, $realEnd); 
       foreach($period as $date) {                  
                $array[] = $date->format($format);  
                
            }

        if (!is_array($array)) { 
                return FALSE; 
        } 

            $result = array(); 
            $a = [];
            array_push($a, $array);
            foreach ($a as $key => $value) { 
              
               // print_r($value);
                if (is_array($value)) { 
                  $result = array_merge($result, array_flatten($value)); 
                } 
                
              } 
              //return ;     
        print_r($result);

    }

getting output is:

Array ( [0] => 2020-11-21 [1] => 2020-11-22 [2] => 2020-11-23 ) Array ( [0] => 2020-10-11 [1] => 2020-10-12 [2] => 2020-10-13 [3] => 2020-10-14 [4] => 2020-10-15 )

Please help me to get the result in single array.

1

There are 1 answers

0
AntonioSk On
array_merge ([ array $... ] ) : array

Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.

If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.

Values in the input arrays with numeric keys will be renumbered with incrementing keys starting from zero in the result array.