【问题标题】:php array merge not giving expected/desired resultsphp数组合并没有给出预期/期望的结果
【发布时间】:2020-03-25 16:43:16
【问题描述】:

使用以下代码,我可以生成 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])作为键,然后在 $arr1 中匹配键的末尾添加来自 $ 的匹配键的内容arr2 后面跟着数字。

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

【问题讨论】:

  • 对于未来的任何问题,请尝试生成minimal 示例。您的观点可以用不超过 2 或 3 个值的示例数组来说明。
  • @El_Vanja 注意到。
  • array_merge 不是您想要的,如果您正确阅读了documentation。看起来您需要使用循环编写自定义函数。
  • 您的最终结果是 2 个数组还是只是 1 个合并数组?
  • 我认为它与使用的 previ 相似。类似于 foreach($csv, $csv2 as $key => $value){ if(!array_key_exists($value[1],$arr2)){ $arr2[$value[1]] = []; } $arr2[$value[1]] = array_merge($arr2[$value[1]],$value); } 但我真的不知道要改变什么。

标签: php arrays array-merge


【解决方案1】:

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

【讨论】:

  • 谢谢,不仅完全达到了我的需要,而且采用了更好的启动方法。非常感谢。
【解决方案2】:

使用array_merge_recursive 代替array_merge

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

【讨论】:

  • 我刚刚尝试过这个,它同样不受欢迎的结果,仍然没有复制订单 ID(键),它仍然不是一个连续编号的数组,而是它在两者周围包装了一个新数组,但是保持编号为旧编号。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-01-12
  • 1970-01-01
  • 2020-05-29
  • 1970-01-01
  • 2019-06-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多