Search code examples
phparraysarray-merge

php array merge not giving expected/desired results


With the following code I am able to generate 2 arrays..

function readCSV($csvFile){
    $file_handle = fopen($csvFile, 'r');

    //ignores first line of csv
    fgetcsv($file_handle);


    while (!feof($file_handle) ) {
        $line_of_text[] = fgetcsv($file_handle, 1024);
    }
    fclose($file_handle);
    return $line_of_text;
}

// Set path to CSV file
$csvFile = 'csv1.csv';
$csvFile2 = 'csv2.csv'; 
$csv = readCSV($csvFile);
$csv2 = readCSV($csvFile2);

echo '<pre>';
$arr = [];
//$csv is your array
foreach($csv as $key => $value){
  if(!array_key_exists($value[24],$arr)){
    $arr[$value[24]] = [];
  }
  $arr[$value[24]] = array_merge($arr[$value[24]],$value);  
}
//ignores last item in array
array_pop($arr);

$arr2 = [];
//$csv2 is your array
foreach($csv2 as $key => $value){
  if(!array_key_exists($value[1],$arr2)){
    $arr2[$value[1]] = [];
  }
  $arr2[$value[1]] = array_merge($arr2[$value[1]],$value);  
}

echo '<pre>'; 
print_r($arr);
print_r($arr2);
echo '</pre>'; 

This outputs the arrays as follows.. Array 1 ($arr)

Array
(
    [1593608448] => Array
        (
            [0] => 03/25/20
            [1] => Vinyl Decal Sticker
            [2] => 
            [3] => 1
            [4] => 2.85
            [5] => 
            [6] => 
            [7] => 0.00
            [8] => 0.00
            [9] => 2.49
            [10] => 0
            [11] => 2.85
            [12] => GBP
            [13] => 1829006957
            [14] => 627718667
            [15] => 03/25/2020
            [16] => 03/25/2020
            [17] => John Smith
            [18] => Any Street
            [19] => 
            [20] => Somewhere
            [21] => Someplace
            [22] => PC0 DE
            [23] => United Kingdom
            [24] => 1593608448
            [25] => Colour:Yellow
            [26] => online
            [27] => listing
            [28] => online_cc
            [29] => 
            [30] => 
            [31] => 0
        )

)

And array 2 ($arr2)

Array
(
    [1593608448] => Array
        (
            [0] => 03/25/20
            [1] => 1593608448
            [2] => 
            [3] => John Smith
            [4] => John
            [5] => Smith
            [6] => 1
            [7] => Credit Card
            [8] => 03/25/20
            [9] => Any Street
            [10] => 
            [11] => Somewhere
            [12] => Someplace
            [13] => PC 0DE
            [14] => United Kingdom
            [15] => GBP
            [16] => 2.85
            [17] => 
            [18] => 
            [19] => 0.00
            [20] => 0.00
            [21] => 2.49
            [22] => 0
            [23] => 5.88
            [24] => 
            [25] => 0.44
            [26] => 4.9
            [27] => 
            [28] => 
            [29] => 
            [30] => John Smith
            [31] => online
            [32] => online_cc
            [33] => 
            [34] => 
        )

)

I have attempted to merge these arrays with..

$merged = array_merge($arr,$arr2);
print_r($merged);

This outputs as..

Array
(
    [0] => Array
        (
            [0] => 03/25/20
            [1] => Vinyl Decal Sticker
            [2] => 
            [3] => 1
            [4] => 2.85
            [5] => 
            [6] => 
            [7] => 0.00
            [8] => 0.00
            [9] => 2.49
            [10] => 0
            [11] => 2.85
            [12] => GBP
            [13] => 1829006957
            [14] => 627718667
            [15] => 03/25/2020
            [16] => 03/25/2020
            [17] => John Smith
            [18] => Any Street
            [19] => 
            [20] => Somewhere
            [21] => Someplace
            [22] => PC0 DE
            [23] => United Kingdom
            [24] => 1593608448
            [25] => Colour:Yellow
            [26] => online
            [27] => listing
            [28] => online_cc
            [29] => 
            [30] => 
            [31] => 0
        )

    [1] => Array
        (
            [0] => 03/25/20
            [1] => 1593608448
            [2] => 
            [3] => John Smith
            [4] => John
            [5] => Smith
            [6] => 1
            [7] => Credit Card
            [8] => 03/25/20
            [9] => Any Street
            [10] => 
            [11] => Somewhere
            [12] => Someplace
            [13] => PC 0DE
            [14] => United Kingdom
            [15] => GBP
            [16] => 2.85
            [17] => 
            [18] => 
            [19] => 0.00
            [20] => 0.00
            [21] => 2.49
            [22] => 0
            [23] => 5.88
            [24] => 
            [25] => 0.44
            [26] => 4.9
            [27] => 
            [28] => 
            [29] => 
            [30] => John Smith
            [31] => online
            [32] => online_cc
            [33] => 
            [34] => 
        )

)

When I wanted/expected..

Array
(
    [1593608448] => Array
        (
            [0] => 03/25/20
            [1] => Vinyl Decal Sticker
            [2] => 
            [3] => 1
            [4] => 2.85
            [5] => 
            [6] => 
            [7] => 0.00
            [8] => 0.00
            [9] => 2.49
            [10] => 0
            [11] => 2.85
            [12] => GBP
            [13] => 1829006957
            [14] => 627718667
            [15] => 03/25/2020
            [16] => 03/25/2020
            [17] => John Smith
            [18] => Any Street
            [19] => 
            [20] => Somewhere
            [21] => Someplace
            [22] => PC0 DE
            [23] => United Kingdom
            [24] => 1593608448
            [25] => Colour:Yellow
            [26] => online
            [27] => listing
            [28] => online_cc
            [29] => 
            [30] => 
            [31] => 0
            [32] => 03/25/20
            [33] => 1593608448
            [34] => 
            [35] => John Smith
            [36] => John
            [37] => Smith
            [38] => 1
            [39] => Credit Card
            [40] => 03/25/20
            [41] => Any Street
            [42] => 
            [43] => Somewhere
            [44] => Someplace
            [45] => PC 0DE
            [46] => United Kingdom
            [47] => GBP
            [48] => 2.85
            [49] => 
            [50] => 
            [51] => 0.00
            [52] => 0.00
            [53] => 2.49
            [54] => 0
            [55] => 5.88
            [56] => 
            [57] => 0.44
            [58] => 4.9
            [59] => 
            [60] => 
            [61] => 
            [62] => John Smith
            [63] => online
            [64] => online_cc
            [65] => 
            [66] => 
        )

)

So keeping the order id (which is always [24] in 1 arr1 and [2] in the arr2) as the key then add on to the end of the matching key in $arr1 the contents of the matching key from $arr2 with the numbers just following on.

I have looked at many similar questions and answers on here but not getting the results I require. Or another way of putting it is it will check if [24] and [2] match and then append on to the end


Solution

  • It would be better to merge the data as you read it. I was going to change your read so that it read the CSV as it created the arrays, but I have left the readCSV() as it is - except to add array_filter() to remove empty elements.

    Rather than create the two arrays and then merge them, I've change the second CSV loop to check $arr and add the data at this point...

    function readCSV($csvFile){
        $file_handle = fopen($csvFile, 'r');
    
        //ignores first line of csv
        fgetcsv($file_handle);
    
    
        while (!feof($file_handle) ) {
            $line_of_text[] = fgetcsv($file_handle, 1024);
        }
        fclose($file_handle);
    
        $line_of_text = array_filter($line_of_text);
        return $line_of_text;
    }
    
    $csvFile = 'csv1.csv';
    $csvFile2 = 'csv2.csv';
    $csv = readCSV($csvFile);
    $csv2 = readCSV($csvFile2);
    
    echo '<pre>';
    $arr = [];
    //$csv is your array
    foreach($csv as $key => $value){
        if(!array_key_exists($value[24],$arr)){
            $arr[$value[24]] = [];
        }
        $arr[$value[24]] = array_merge($arr[$value[24]],$value);
    }
    
    foreach($csv2 as $key => $value){
        if(!array_key_exists($value[1],$arr)){
            $arr[$value[1]] = [];
        }
        $arr[$value[1]] = array_merge($arr[$value[1]],$value);
    }
    
    echo '<pre>';
    print_r($arr);
    echo '</pre>';