Merge/combine(不确定我需要哪个?)php 数组,如果键 [any][0] 是相同的数字

Merge/combine (not sure which i need?) the php array if keys [any][0] are the same number

使用以下代码,我可以将上传的 CSV 文件转换为 php 数组..

    <?PHP
function readCSV($csvFile){
    $file_handle = fopen($csvFile, 'r');
    fgetcsv($file_handle);
    fgetcsv($file_handle);
    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 = 'test.csv';

$csv = readCSV($csvFile);
?>

然后我可以在我的页面上查看数组...

<?php echo '<pre>';
print_r($csv);
echo '</pre>'; ?> 

这将输出如下数组(看起来像真实数据但已匿名化)..

Array
(
    [0] => Array
        (
            [0] => 15304
            [1] => 15-04478-27247
            [2] => ersmith0
            [3] => eric smith
            [4] => myemail@gmail.com
            [5] => 
            [6] => 2 Any Close
            [7] => 
            [8] => Manchester
            [9] => Greater Manchester
            [10] => M1 5BB
            [11] => United Kingdom
            [12] => eric smith
            [13] => +44 7719 123456
            [14] => 27 Any Close
            [15] => 
            [16] => Manchester
            [17] => Greater Manchester
            [18] => M1 5BB
            [19] => United Kingdom
            [20] => 
            [21] => 
            [22] => 
            [23] => 
            [24] => 2
            [25] => £3.90
            [26] => £0.00
            [27] => 
            [28] => £0.00
            [29] => £0.00
            [30] => £3.90
            [31] => No
            [32] => PayPal
            [33] => 30-Jan-20
            [34] => 30-Jan-20
            [35] => 
            [36] => 
            [37] => 
            [38] => 31-Jan-20
            [39] => 
            [40] => 
            [41] => 
            [42] => 5LD0123456789U
            [43] => Royal Mail 1st Class
            [44] => 
            [45] => 
            [46] => 
            [47] => No
            [48] => 
            [49] => No
            [50] => 
            [51] => No
        )

    [1] => Array
        (
            [0] => 15304
            [1] => 15-04478-27247
            [2] => ersmith0
            [3] => 
            [4] => 
            [5] => 
            [6] => 
            [7] => 
            [8] => 
            [9] => 
            [10] => 
            [11] => 
            [12] => 
            [13] => 
            [14] => 
            [15] => 
            [16] => 
            [17] => 
            [18] => 
            [19] => 
            [20] => 302804801330
            [21] => CAUTION DRIVEN BY A BIKER  - Fun Vinyl Decal Sticker 4x4/Car/Lorry/Van/Camper
            [22] => 
            [23] => No
            [24] => 1
            [25] => £1.95
            [26] => 
            [27] => 
            [28] => 
            [29] => 
            [30] => 
            [31] => 
            [32] => 
            [33] => 30-Jan-20
            [34] => 
            [35] => 
            [36] => 
            [37] => 
            [38] => 31-Jan-20
            [39] => No
            [40] => 
            [41] => 
            [42] => 
            [43] => 
            [44] => 
            [45] => 1711351782020
            [46] => 
            [47] => 
            [48] => 
            [49] => 
            [50] => 
            [51] => 
        )

    [2] => Array
        (
            [0] => 15304
            [1] => 15-04478-27247
            [2] => ersmith0
            [3] => 
            [4] => 
            [5] => 
            [6] => 
            [7] => 
            [8] => 
            [9] => 
            [10] => 
            [11] => 
            [12] => 
            [13] => 
            [14] => 
            [15] => 
            [16] => 
            [17] => 
            [18] => 
            [19] => 
            [20] => 302804815489
            [21] => Warning Biker With an Attitude Annoy At Your Own Risk Car Vinyl Decal Sticker
            [22] => 
            [23] => No
            [24] => 1
            [25] => £1.95
            [26] => 
            [27] => 
            [28] => 
            [29] => 
            [30] => 
            [31] => 
            [32] => 
            [33] => 30-Jan-20
            [34] => 
            [35] => 
            [36] => 
            [37] => 
            [38] => 31-Jan-20
            [39] => No
            [40] => 
            [41] => 
            [42] => 
            [43] => 
            [44] => 
            [45] => 1711351781020
            [46] => 
            [47] => 
            [48] => 
            [49] => 
            [50] => 
            [51] => 
        )

    [3] => Array
        (
            [0] => 15305
            [1] => 25-04445-36722
            [2] => pod69
            [3] => mart badger
            [4] => martsemailddr34@gmail.com
            [5] => 
            [6] => Via Riccione N. 35/30
            [7] => 
            [8] => Sa Tiacca Quartu S. Elena
            [9] => CA
            [10] => 09445
            [11] => Italy
            [12] => Badger Mart
            [13] => +39 987 654 3211
            [14] => Via Riccione N. 35/30
            [15] => 
            [16] => Sa Tiacca Quartu S. Elena
            [17] => CA
            [18] => 09445
            [19] => Italy
            [20] => 302667176336
            [21] => 2x Funny Car & Van or Camper WV Eyelash Vinyl Sticker Decal, Signs - Any Colour!
            [22] => 
            [23] => No
            [24] => 2
            [25] => £1.99
            [26] => £3.14
            [27] => 
            [28] => £0.00
            [29] => £0.00
            [30] => £7.12
            [31] => No
            [32] => PayPal
            [33] => 23-Jan-20
            [34] => 23-Jan-20
            [35] => 24-Jan-20
            [36] => 
            [37] => 
            [38] => 24-Jan-20
            [39] => Yes
            [40] => 
            [41] => 
            [42] => 25C157351T6888860U
            [43] => Royal Mail International Standard
            [44] => 
            [45] => 1709118816020
            [46] => 
            [47] => No
            [48] => 
            [49] => No
            [50] => 
            [51] => No
        )

    [4] => Array
        (
            [0] => 15306
            [1] => 20-04482-41274
            [2] => wideboiy38887
            [3] => Paul Lastname
            [4] => wideboiysemail@hotmail.co.uk
            [5] => Yellow please
            [6] => 10
            [7] => Broomstick Lane
            [8] => Okehampton
            [9] => Devon
            [10] => EX19 2FT
            [11] => United Kingdom
            [12] => Paul Lastname
            [13] => +44 7866 401983
            [14] => 105
            [15] => Broomstick Lane
            [16] => Okehampton
            [17] => Devon
            [18] => EX19 2FT
            [19] => United Kingdom
            [20] => 302755568952
            [21] => Junglist Soldier Car Window Laptop Wall Gift Drum Bass dnb Jungle Vinyl Sticker
            [22] => 
            [23] => No
            [24] => 1
            [25] => £1.99
            [26] => £0.00
            [27] => 
            [28] => £0.00
            [29] => £0.00
            [30] => £1.99
            [31] => No
            [32] => PayPal
            [33] => 01-Feb-20
            [34] => 01-Feb-20
            [35] => 03-Feb-20
            [36] => 
            [37] => 
            [38] => 
            [39] => No
            [40] => 
            [41] => 
            [42] => 7SD68673EE789782X
            [43] => Royal Mail 1st Class
            [44] => 
            [45] => 1711667179020
            [46] => 
            [47] => No
            [48] => 
            [49] => No
            [50] => 
            [51] => No
        )

    <!-And so on.....>

如果您查看 [0]、[1] 和 [2],您会发现它们都属于同一销售记录编号(因此 [0][0]、[1][0] 和 [ 2][0] 是相同的编号)(但项目 [3][0] 和 [4][0] 是单独的订单/不同的编号)。 (请记住,虽然这只是一个示例,但数据会发生变化,可能有 100 个订单,每个订单号可能有 1 件或几件商品。)

我想合并具有相同销售记录编号[任意][0]

的订单数组

所以像....

如果 [any][0] 相互匹配则合并数组

这意味着匹配的订单号的所有信息将在每个订单号的 1 个数组中。

我想保留所有数据(忽略空白)但将它们 merged/combined 作为子数组放入匹配的顶级数组中,例如.. 如果 [1][0] 匹配 [2][ 0] 从 [2][0] 到 [2][51] 的所有数据(不为空或相同)都应添加到 [1] 上,这样它就变成了...

Array
(
    [0] => Array
        (
            [0] => 15304
            [1] => 15-04478-27247
            [2] => ersmith0
            [3] => eric smith
            [4] => myemail@gmail.com
            [5] => 
            [6] => 2 Any Close
            [7] => 
            [8] => Manchester
            [9] => Greater Manchester
            [10] => M1 5BB
            [11] => United Kingdom
            [12] => eric smith
            [13] => +44 7719 123456
            [14] => 27 Any Close
            [15] => 
            [16] => Manchester
            [17] => Greater Manchester
            [18] => M1 5BB
            [19] => United Kingdom
            [20] => 
            [21] => Array (
                [0] => CAUTION DRIVEN BY A BIKER  - Fun Vinyl Decal Sticker 4x4/Car/Lorry/Van/Camper
                [1] => Warning Biker With an Attitude Annoy At Your Own Risk Car Vinyl Decal Sticker
            )
            [22] => 
            [23] => 
            [24] => Array (
                [0] => 1
                [1] => 1
            ) 
            [25] => Array (
                [0] => 1.95
                [1] => 1.95
            ) 
            [26] => £0.00
            [27] => 
            [28] => £0.00
            [29] => £0.00
            [30] => £3.90
            [31] => No
            [32] => PayPal
            [33] => 30-Jan-20
            [34] => 30-Jan-20
            [35] => 
            [36] => 
            [37] => 
            [38] => 31-Jan-20
            [39] => 
            [40] => 
            [41] => 
            [42] => 5LD0123456789U
            [43] => Royal Mail 1st Class
            [44] => 
            [45] => 
            [46] => 
            [47] => No
            [48] => 
            [49] => No
            [50] => 
            [51] => No
        )

    [3] => Array
        (
            [0] => 15305
            [1] => 25-04445-36722
            [2] => pod69
            [3] => mart badger
            [4] => martsemailddr34@gmail.com
            [5] => 
            [6] => Via Riccione N. 35/30
            [7] => 
            [8] => Sa Tiacca Quartu S. Elena
            [9] => CA
            [10] => 09445
            [11] => Italy
            [12] => Badger Mart
            [13] => +39 987 654 3211
            [14] => Via Riccione N. 35/30
            [15] => 
            [16] => Sa Tiacca Quartu S. Elena
            [17] => CA
            [18] => 09445
            [19] => Italy
            [20] => 302667176336
            [21] => 2x Funny Car & Van or Camper WV Eyelash Vinyl Sticker Decal, Signs - Any Colour!
            [22] => 
            [23] => No
            [24] => 2
            [25] => £1.99
            [26] => £3.14
            [27] => 
            [28] => £0.00
            [29] => £0.00
            [30] => £7.12
            [31] => No
            [32] => PayPal
            [33] => 23-Jan-20
            [34] => 23-Jan-20
            [35] => 24-Jan-20
            [36] => 
            [37] => 
            [38] => 24-Jan-20
            [39] => Yes
            [40] => 
            [41] => 
            [42] => 25C157351T6888860U
            [43] => Royal Mail International Standard
            [44] => 
            [45] => 1709118816020
            [46] => 
            [47] => No
            [48] => 
            [49] => No
            [50] => 
            [51] => No
        )

    [4] => Array
        (
            [0] => 15306
            [1] => 20-04482-41274
            [2] => wideboiy38887
            [3] => Paul Lastname
            [4] => wideboiysemail@hotmail.co.uk
            [5] => Yellow please
            [6] => 10
            [7] => Broomstick Lane
            [8] => Okehampton
            [9] => Devon
            [10] => EX19 2FT
            [11] => United Kingdom
            [12] => Paul Lastname
            [13] => +44 7866 401983
            [14] => 105
            [15] => Broomstick Lane
            [16] => Okehampton
            [17] => Devon
            [18] => EX19 2FT
            [19] => United Kingdom
            [20] => 302755568952
            [21] => Junglist Soldier Car Window Laptop Wall Gift Drum Bass dnb Jungle Vinyl Sticker
            [22] => 
            [23] => No
            [24] => 1
            [25] => £1.99
            [26] => £0.00
            [27] => 
            [28] => £0.00
            [29] => £0.00
            [30] => £1.99
            [31] => No
            [32] => PayPal
            [33] => 01-Feb-20
            [34] => 01-Feb-20
            [35] => 03-Feb-20
            [36] => 
            [37] => 
            [38] => 
            [39] => No
            [40] => 
            [41] => 
            [42] => 7SD68673EE789782X
            [43] => Royal Mail 1st Class
            [44] => 
            [45] => 1711667179020
            [46] => 
            [47] => No
            [48] => 
            [49] => No
            [50] => 
            [51] => No
        )

    <!-And so on.....>

我希望这是有道理的,这就是我认为我需要的。

我想要实现的是为每个顶级数组制作一个 "for each loop" 以便我可以制作发票页面,然后我可以在其中将每个顶级数组用作单独的 invoice/order 页面在该页面的 html 标记中。

请尝试以下代码。有效。

<?php
    function readCSV($csvFile)
    {
        $line_of_text = [];
        $file_handle = fopen($csvFile, 'r');
        while (!feof($file_handle)) {
            $tmp = fgetcsv($file_handle, 1024);

            if (isset($line_of_text[$tmp[0]])) {
                foreach ($tmp as $k => $v) {

                    if (array_key_exists($k, $line_of_text[$tmp[0]])) {
                        if (!is_array($line_of_text[$tmp[0]][$k])) {
                            $kVal = $line_of_text[$tmp[0]][$k];

                            $line_of_text[$tmp[0]][$k] = [];
                            $line_of_text[$tmp[0]][$k][] = $kVal;
                        }

                        $line_of_text[$tmp[0]][$k][] = $v;
                        $line_of_text[$tmp[0]][$k] = array_unique($line_of_text[$tmp[0]][$k]);
                        $line_of_text[$tmp[0]][$k] = array_filter($line_of_text[$tmp[0]][$k]);

                        if (count($line_of_text[$tmp[0]][$k]) == 1) {
                            $line_of_text[$tmp[0]][$k] = array_values($line_of_text[$tmp[0]][$k]);
                            $line_of_text[$tmp[0]][$k] = $line_of_text[$tmp[0]][$k][0];
                        }

                        if (empty($line_of_text[$tmp[0]][$k])) {
                            $line_of_text[$tmp[0]][$k] = null;
                        }

                    } else {
                        $line_of_text[$tmp[0]][$k] = null;
                    }
                }
                $line_of_text[$tmp[0]][0] = $tmp[0];

            } else {
                $line_of_text[$tmp[0]] = $tmp;
            }

        }

        fclose($file_handle);
        return array_filter(array_values($line_of_text));
    }

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

    echo "<pre>";
    print_r($csv);

?>

=>输出

Array
(
    [0] => Array
        (
            [0] => 15304
            [1] => 15-04478-27247
            [2] => ersmith0
            [3] => eric smith
            [4] => myemail@gmail.com
            [5] => 
            [6] => 2 Any Close
            [7] => 
            [8] => Manchester
            [9] => Greater Manchester
            [10] => M1 5BB
            [11] => United Kingdom
            [12] => eric smith
            [13] => +44 7719 123456
            [14] => 27 Any Close
            [15] => 
            [16] => Manchester
            [17] => Greater Manchester
            [18] => M1 5BB
            [19] => United Kingdom
            [20] => Array
                (
                    [0] => 302804801330
                    [1] => 302804815489
                )

            [21] => Array
                (
                    [0] => CAUTION DRIVEN BY A BIKER  - Fun Vinyl Decal Sticker 4x4/Car/Lorry/Van/Camper
                    [1] => Warning Biker With an Attitude Annoy At Your Own Risk Car Vinyl Decal Sticker
                )

            [22] => 
            [23] => No
            [24] => Array
                (
                    [0] => 2
                    [1] => 1
                )

            [25] => Array
                (
                    [0] => £3.90
                    [1] => £1.95
                )

            [26] => £0.00
            [27] => 
            [28] => £0.00
            [29] => £0.00
            [30] => £3.90
            [31] => No
            [32] => PayPal
            [33] => 30-Jan-20
            [34] => 30-Jan-20
            [35] => 
            [36] => 
            [37] => 
            [38] => 31-Jan-20
            [39] => No
            [40] => 
            [41] => 
            [42] => 5LD0123456789U
            [43] => Royal Mail 1st Class
            [44] => 
            [45] => Array
                (
                    [0] => 1711351782020
                    [1] => 1711351781020
                )

            [46] => 
            [47] => No
            [48] => 
            [49] => No
            [50] => 
            [51] => No
        )

    [1] => Array
        (
            [0] => 15305
            [1] => 25-04445-36722
            [2] => pod69
            [3] => mart badger
            [4] => martsemailddr34@gmail.com
            [5] => 
            [6] => Via Riccione N. 35/30
            [7] => 
            [8] => Sa Tiacca Quartu S. Elena
            [9] => CA
            [10] => 09445
            [11] => Italy
            [12] => Badger Mart
            [13] => +39 987 654 3211
            [14] => Via Riccione N. 35/30
            [15] => 
            [16] => Sa Tiacca Quartu S. Elena
            [17] => CA
            [18] => 09445
            [19] => Italy
            [20] => 302667176336
            [21] => 2x Funny Car & Van or Camper WV Eyelash Vinyl Sticker Decal, Signs - Any Colour!
            [22] => 
            [23] => No
            [24] => 2
            [25] => £1.99
            [26] => £3.14
            [27] => 
            [28] => £0.00
            [29] => £0.00
            [30] => £7.12
            [31] => No
            [32] => PayPal
            [33] => 23-Jan-20
            [34] => 23-Jan-20
            [35] => 24-Jan-20
            [36] => 
            [37] => 
            [38] => 24-Jan-20
            [39] => Yes
            [40] => 
            [41] => 
            [42] => 25C157351T6888860U
            [43] => Royal Mail International Standard
            [44] => 
            [45] => 1709118816020
            [46] => 
            [47] => No
            [48] => 
            [49] => No
            [50] => 
            [51] => No
        )

    [2] => Array
        (
            [0] => 15306
            [1] => 20-04482-41274
            [2] => wideboiy38887
            [3] => Paul Lastname
            [4] => wideboiysemail@hotmail.co.uk
            [5] => Yellow please
            [6] => 10
            [7] => Broomstick Lane
            [8] => Okehampton
            [9] => Devon
            [10] => EX19 2FT
            [11] => United Kingdom
            [12] => Paul Lastname
            [13] => +44 7866 401983
            [14] => 105
            [15] => Broomstick Lane
            [16] => Okehampton
            [17] => Devon
            [18] => EX19 2FT
            [19] => United Kingdom
            [20] => 302755568952
            [21] => Junglist Soldier Car Window Laptop Wall Gift Drum Bass dnb Jungle Vinyl Sticker
            [22] => 
            [23] => No
            [24] => 1
            [25] => £1.99
            [26] => £0.00
            [27] => 
            [28] => £0.00
            [29] => £0.00
            [30] => £1.99
            [31] => No
            [32] => PayPal
            [33] => 01-Feb-20
            [34] => 01-Feb-20
            [35] => 03-Feb-20
            [36] => 
            [37] => 
            [38] => 
            [39] => No
            [40] => 
            [41] => 
            [42] => 7SD68673EE789782X
            [43] => Royal Mail 1st Class
            [44] => 
            [45] => 1711667179020
            [46] => 
            [47] => No
            [48] => 
            [49] => No
            [50] => 
            [51] => No
        )

)

你可以像这样处理数组

$arr = [];
//$csv is your array
foreach($csv as $key => $value){
  $arr[$value[0]][] = $value;  
}
print_r($arr);

此代码将附加具有相同第 [0] 个索引的所有嵌套数组。

然后您可以使用其他 php 函数使您的数组独一无二。

如果你想要合并数组的结果,你可以尝试下面的代码。

$arr = [];
//$csv is your array
foreach($csv as $key => $value){
  if(!array_key_exists($value[0],$arr)){
    $arr[$value[0]] = [];
  }
  $arr[$value[0]] = array_merge($arr[$value[0]],$value);  
}
print_r($arr);

我没有测试过代码,所以如果有错误请更正。

如果发生以下情况,我认为这可能会导致与您希望的不同:

假设我们有 3 条这样的记录:

$original = [0] => [
  [0] => 'id0',
  [1] => 'name 0',
  [2] => 'street 0'
],
[1] => [
  [0] => 'id0',
  [1] => 'name 1',
  [2] => 
],
[2] => [
  [0] => 'id0',
  [1] =>,
  [2] => 'street 2'
]

如果我按照我认为的意思进行操作,您将得到以下结构:

$new = [0] => [
  [0] => 'id0',
  [1] => [
    [0] => 'name 0',
    [1] => 'name 1'
  ],
  [2] => [
    [0] => 'street 0',
    [1] => 'street 2'
  ]
]

你能看到 $new[0][1][1] 来自 $original[1][1],而在数组 $new[0][2][1] 中的相同位置来自 $original[2][2]

我认为问题是你现在将 $original[X][1] 与 $original[X][2] 对齐,如果你以某种 table / overview 的形式打印出来彼此相邻但不相关。

==

至于如何解决你的问题,我建议是这样的:

function readCsv($csvFileName) {
    $file_handle = fopen($csvFile, 'r');
    $records = [];
    fgetcsv($file_handle);
    while (!feof($file_handle) ) {
        $line = fgetcsv($file_handle, 1024);
        $id = $line[0];
        if (!$records[$id]) $records[$id] = [];
        $records[$id][] = $line;
    }
    fclose($file_handle);
    return $records;
}

function printCsvColumn($rowName, $sales) {
    $uniqueSales = array_unique($sales);
    $uniqueNotNullSales = array_filter($uniqueSales, 'strlen');
    foreach ($uniqueNotNullSales as $sale) {
        echo $rowName . ': ' . $sale[$rowName];
    }
}

function printCsv($records) {
    foreach ($records as $sales) {
        foreach ($sales[0] as $rowName=>$rowValue) {
            printCsvColumn($rowName, $sales);
        }
    }
}

如果您了解 OOP,您可以制作 类 和对象来为您表示此逻辑,这样您就不会在整个代码中使用此功能获取和迭代数据。