php 数组合并未给出 expected/desired 结果

php array merge not giving expected/desired results

使用以下代码我可以生成 2 个数组..

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>'; 

这将输出数组如下.. 数组 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
        )

)

和数组 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] => 
        )

)

我试图将这些数组与..

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

这输出为..

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

)

当我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] => 
        )

)

因此,将订单 ID(在 1 arr1 中始终为 [24],在 arr2 中始终为 [2])作为键,然后将匹配键的内容添加到 $arr1 中匹配键的末尾来自 $arr2,紧随其后的是数字。

我在这里查看了许多类似的问题和答案,但没有得到我需要的结果。 或者另一种表达方式是检查 [24] 和 [2] 是否匹配,然后附加到末尾

使用array_merge_recursive代替array_merge

$merged = array_merge_recursive($arr,$arr2);

最好边读边合并数据。我打算更改您的读取,以便它在创建数组时读取 CSV,但我保留了 readCSV() 原样 - 除了添加 array_filter() 以删除空元素。

我没有创建两个数组然后合并它们,而是更改了第二个 CSV 循环以检查 $arr 并在此时添加数据...

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>';