代码之家  ›  专栏  ›  技术社区  ›  Solomon Antoine

PHP Array_Combine()合并所有元素时出错

  •  0
  • Solomon Antoine  · 技术社区  · 8 年前

    我试图在PHP中组合两个数组,并使用array combine函数,它组合了所有元素,除了最后一个看起来很模糊的元素。我正在从文本文件导入数据。这是从文本文件导入的数据:

    ASN|PO Date|Ship Date|PO Number|Quantity Ordered|Quantity Shipped|Product ID|Unit of Measure|Ship To Name|Ship To Address1|Ship To Address2|Ship To City|Ship To State|Ship To Zip Code|Tracking Number|Carrier|Order Number
    
    ASNData|01/01/17|01/01/2017|1356061|1|1|LEU 171565|EA|ASHLEY DAVIS|15155 23RD AVE N||MINNEAPOLIS|MN|55447|9410811899223445945673|UP39|1234567
    

    目标是创建一个关联数组,标签作为键,结果作为值。

    这是我的代码:

    <?php
    $content = Storage::disk('billHicksAcknowledgment')->get('856_Advanced_Shipping.txt');
    $file = explode("\n", $content);
    $labels = $file[0];
    $key = explode("|", $labels);
    $result = $file[1];
    $value = explode("|", $result);
    $order = array_combine($key, $value);
    print_r($order);
    

    当我运行代码时,这就是它在我的终端中发出的回声:

    Array
    (
        [ASN] => ASNData
        [PO Date] => 01/01/17
        [Ship Date] => 01/01/2017
        [PO Number] => 1356061
        [Quantity Ordered] => 1
        [Quantity Shipped] => 1
        [Product ID] => LEU 171565
        [Unit of Measure] => EA
        [Ship To Name] => John Doe
        [Ship To Address1] => 15155 23RD AVE NW
        [Ship To Address2] => 
        [Ship To City] => MINNEAPOLIS
        [Ship To State] => MN
        [Ship To Zip Code] => 55442
        [Tracking Number] => 9410811899223445945673
        [Carrier] => UP39
    ] => 1234567umber
    )
    

    如果您注意到数组的最后一个元素没有正确打印。当我试着调试时,我分别打印了每个数组,这会返回所有看起来很好的结果。

    代码如下:

    $content = Storage::disk('billHicksAcknowledgment')->get('856_Advanced_Shipping.txt');
    $file = explode("\n", $content);
    $labels = $file[0];
    $key = explode("|", $labels);
    die(print_r($key)); //code stops and prints first array here
    $result = $file[1];
    $value = explode("|", $result);
    $order = array_combine($key, $value);
    print_r($order);
    

    结果如下:

    Array
    (
        [0] => ASN
        [1] => PO Date
        [2] => Ship Date
        [3] => PO Number
        [4] => Quantity Ordered
        [5] => Quantity Shipped
        [6] => Product ID
        [7] => Unit of Measure
        [8] => Ship To Name
        [9] => Ship To Address1
        [10] => Ship To Address2
        [11] => Ship To City
        [12] => Ship To State
        [13] => Ship To Zip Code
        [14] => Tracking Number
        [15] => Carrier
        [16] => Order Number
    )
    1
    

    指纹没问题吧?现在让我打印第二个数组

    代码如下:

    $content = Storage::disk('billHicksAcknowledgment')->get('856_Advanced_Shipping.txt');
    $file = explode("\n", $content);
    $labels = $file[0];
    $key = explode("|", $labels);
    $result = $file[1];
    $value = explode("|", $result);
    die(print_r($value)); //code stops and prints second array
    $order = array_combine($key, $value);
    print_r($order);
    

    结果是:

    Array
    (
        [0] => ASNData
        [1] => 01/01/17
        [2] => 01/01/2017
        [3] => 1356061
        [4] => 1
        [5] => 1
        [6] => LEU 171565
        [7] => EA
        [8] => John Doe
        [9] => 15155 23RD AVE N
        [10] => 
        [11] => MINNEAPOLIS
        [12] => MN
        [13] => 55442
        [14] => 9410811899223445945673
        [15] => UP39
        [16] => 1234567
    )
    1
    

    但当我把它们结合起来时,结果总是:

    Array
    (
        [ASN] => ASNData
        [PO Date] => 01/01/17
        [Ship Date] => 01/01/2017
        [PO Number] => 1356061
        [Quantity Ordered] => 1
        [Quantity Shipped] => 1
        [Product ID] => LEU 171565
        [Unit of Measure] => EA
        [Ship To Name] => John Doe
        [Ship To Address1] => 15155 23RD AVE NW
        [Ship To Address2] => 
        [Ship To City] => MINNEAPOLIS
        [Ship To State] => MN
        [Ship To Zip Code] => 55447
        [Tracking Number] => 9410811899223445945673
        [Carrier] => UP39
    ] => 1234567umber
    )
    

    有什么建议吗?

    1 回复  |  直到 8 年前
        1
  •  1
  •   Sean Bright Sean Stinehour    8 年前

    你有 \r\n 文件中的行尾。执行以下操作:

    $key = array_map('trim', explode("|", $labels));
    ...
    $value = array_map('trim', explode("|", $result));
    
    推荐文章