php数组合并未给出预期的/期望的结果

问题描述 投票:0回答:2

使用以下代码,我能够生成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] => 
        )

)

我想要/期望的时间。

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])作为键,然后将$ arr2中匹配键的内容添加到$ arr1中的匹配键末尾后面紧跟着数字。

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

php arrays array-merge
2个回答
2
投票

阅读时最好合并数据。我打算更改您的读取值,以便它在创建数组时读取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>'; 

0
投票

使用array_merge_recursive代替array_merge

$merged = array_merge_recursive($arr,$arr2);
© www.soinside.com 2019 - 2024. All rights reserved.