我正在尝试在PHP中组合两个数组,并使用数组合并功能,它合并了所有元素,除了最后一个似乎出现故障的元素。我正在从文本文件导入数据。这是从文本文件导入的数据:
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
)
有什么建议吗?
答案 0 :(得分:1)
文件中有\r\n
个行尾。这样做:
$key = array_map('trim', explode("|", $labels));
...
$value = array_map('trim', explode("|", $result));