Search code examples
phparraysloopsmergearray-merge

Merge dynamic arrays in php


I have some data that I am processing, and I've reached a stage where I have some arrays that I am merging. My problem is that the amount of arrays I am needing is dynamic and although I got my code to do the work for a set amount of data, I am a bit confused with all those counters and I have repeating code. I think I will need to produce those arrays dynamically somehow but I am not sure how. Here is my code that works perfect when I have 3 arrays (loops) :

$dataonlyarray = array();
$dataonlyarray2 = array();
$dataonlyarray3 = array();
$dataonlyarray4 = array();

        for ($i = 0; $i < sizeof($main_prod_arr); $i++) {
            $dataonlyarray2[] = array_slice($main_prod_arr[$i],0,$startingdatecolumn);
            $dataonlyarray3[] = array_slice($main_prod_arr[$i],0,$startingdatecolumn);  
            $dataonlyarray4[] = array_slice($main_prod_arr[$i],0,$startingdatecolumn);      
            } 

        for ($i = 0; $i < sizeof($main_prod_arr); $i++) {
            array_push($dataonlyarray2[$i],$main_prod_arr[$i][$startingdatecolumn]);    
            array_push($dataonlyarray2[$i],$main_prod_arr[$i][$startingdatecolumn]+1);  
            array_push($dataonlyarray2[$i],substr($headers_arr[$startingdatecolumn],0,10));         
            }   


        for ($i = 0; $i < sizeof($main_prod_arr); $i++) {
            array_push($dataonlyarray3[$i],$main_prod_arr[$i][$startingdatecolumn]+2);
            array_push($dataonlyarray3[$i],$main_prod_arr[$i][$startingdatecolumn]+3);
            array_push($dataonlyarray3[$i],substr($headers_arr[$startingdatecolumn+3],0,10));       
            }   

        for ($i = 0; $i < sizeof($main_prod_arr); $i++) {
            array_push($dataonlyarray4[$i],$main_prod_arr[$i][$startingdatecolumn]+4);
            array_push($dataonlyarray4[$i],$main_prod_arr[$i][$startingdatecolumn]+5);
            array_push($dataonlyarray4[$i],substr($headers_arr[$startingdatecolumn+5],0,10));       
            }           


            $dataonlyarrayall[] = array_merge($dataonlyarray2,$dataonlyarray3,$dataonlyarray4);


echo '<pre>'; print_r($dataonlyarrayall); 
echo '</pre>';

Here is my code if I only have 2 arrays to loop (works fine):

$dataonlyarray = array();
$dataonlyarray2 = array();
$dataonlyarray3 = array();

        for ($i = 0; $i < sizeof($main_prod_arr); $i++) {
            $dataonlyarray2[] = array_slice($main_prod_arr[$i],0,$startingdatecolumn);
            $dataonlyarray3[] = array_slice($main_prod_arr[$i],0,$startingdatecolumn);      
            } 

        for ($i = 0; $i < sizeof($main_prod_arr); $i++) {
            array_push($dataonlyarray2[$i],$main_prod_arr[$i][$startingdatecolumn]);    
            array_push($dataonlyarray2[$i],$main_prod_arr[$i][$startingdatecolumn]+1);  
            array_push($dataonlyarray2[$i],substr($headers_arr[$startingdatecolumn],0,10));         
            }   


        for ($i = 0; $i < sizeof($main_prod_arr); $i++) {
            array_push($dataonlyarray3[$i],$main_prod_arr[$i][$startingdatecolumn]+2);
            array_push($dataonlyarray3[$i],$main_prod_arr[$i][$startingdatecolumn]+3);
            array_push($dataonlyarray3[$i],substr($headers_arr[$startingdatecolumn+3],0,10));       
            }   



            $dataonlyarrayall[] = array_merge($dataonlyarray2,$dataonlyarray3);

echo '<pre>'; print_r($dataonlyarrayall); 
echo '</pre>';

As you will see there are some values that need to increment inside every new array..

Here is a sample of the $main_prod_arr (With 2 products and 3 dates (3 loops required):

Array
(
    [0] => Array
        (
            [0] => 2602721
            [1] => Product1
            [2] => 11 
            [3] => £12
            [4] => 13 
            [5] => £14
            [6] => 15 
            [7] => £16
        )

    [1] => Array
        (
            [0] => 2616122
            [1] => Product2
            [2] => 21 
            [3] => £22
            [4] => 23 
            [5] => £24
            [6] => 25 
            [7] => £26
        )

)

Here is a sample of $headers_arr (With 2 products and 3 dates (3 loops required): Array

(
    [0] => ItemNbr
    [1] => ItemDesc
    [2] => 2016/01/01 Qty
    [3] => 2016/01/01 Sales
    [4] => 2016/01/02 Qty
    [5] => 2016/01/02 Sales
    [6] => 2016/01/03 Qty
    [7] => 2016/01/03 Sales
)

This is the expecrted output (For the above arrays) :

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => Array
                        (
                            [0] => 2602721
                            [1] => Product1
                            [2] => 11 
                            [3] => 12
                            [4] => 2016/01/01
                        )

                    [1] => Array
                        (
                            [0] => 2616122
                            [1] => Product2
                            [2] => 21 
                            [3] => 22
                            [4] => 2016/01/01
                        )

                    [2] => Array
                        (
                            [0] => 2602721
                            [1] => Product1
                            [2] => 13
                            [3] => 14
                            [4] => 2016/01/02
                        )

                    [3] => Array
                        (
                            [0] => 2616122
                            [1] => Product2
                            [2] => 23
                            [3] => 24
                            [4] => 2016/01/02
                        )

                    [4] => Array
                        (
                            [0] => 2602721
                            [1] => Product1
                            [2] => 15
                            [3] => 16
                            [4] => 2016/01/03
                        )

                    [5] => Array
                        (
                            [0] => 2616122
                            [1] => Product2
                            [2] => 25
                            [3] => 26
                            [4] => 2016/01/03
                        )

                )

        )

)

Any help will be appreciated


Solution

  • I think I found the solution. If you have any suggestions to improve please comment :)

    $dataonlyarray = array();
    $dataonlyarraylooped = array();
    $dataonlyarrayall = array();
        for ($z = 0;  $z < sizeof($sec_headers_dates_arr)/2; $z++) {
    
            for ($i = 0; $i < sizeof($main_prod_arr); $i++) {
                $dataonlyarraylooped[] = array_slice($main_prod_arr[$i],0,$startingdatecolumn); 
                } 
    
            for ($i = 0; $i < sizeof($main_prod_arr); $i++) {
                array_push($dataonlyarraylooped[$i],$main_prod_arr[$i][$startingdatecolumn+($z*2)]);    
                array_push($dataonlyarraylooped[$i],$main_prod_arr[$i][$startingdatecolumn+($z*2+1)]);  
                array_push($dataonlyarraylooped[$i],substr($headers_arr[$startingdatecolumn+($z*2+1)],0,10));           
                }   
    
                array_push($dataonlyarrayall,$dataonlyarraylooped); 
                unset($dataonlyarraylooped);
        }
    echo '<pre>'; print_r($dataonlyarrayall); 
    echo '</pre>';