0

I am attempting to combine two arrays in PHP and using the array combine function, it combines all elements except the last element which seems to be glitching out. I am importing data from a text file. This is the data being imported from the text file:

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

The goal is to create an associative array with the labels as the keys and the results as the values.

Here is my code:

<?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);

When I run the code this is what it echos out into my terminal:

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
)

If you notice the last element of the array isn't properly printing. When I tried debugging I printed each array separately which returns everything seemingly fine.

Here is the code:

$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);

Here is the result of that:

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

Prints clean right? Now let me print the second array

Here is the code:

$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);

And Here is the result of that:

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

But when I combine them this is always the result:

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
)

Any suggestions?

1 Answer 1

1

You have \r\n line-endings in your file. Do this:

$key = array_map('trim', explode("|", $labels));
...
$value = array_map('trim', explode("|", $result));
Sign up to request clarification or add additional context in comments.

1 Comment

Dude you are freaking amazing!! I will accept this answer once it lets me. Thanks again!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.