I am trying to make a little batch upload tool to upload tracking numbers.
I have a csv with 2 columns: postal_code, tracking_number
and I have my sql that selects order_id, postal_code:
//import shipping csv with postal codes and tracking numbers and add to array $csv_array = Array(); $file = fopen($_FILES['canada_post_csv']['tmp_name'], 'r'); if($file){ while (($line = fgetcsv($file)) !== FALSE) { //$line is an array of the csv elements array_push($csv_array,$line); } fclose($file); } print_r($csv_array); //select postal codes from orders that are of the status processing $orders_query_raw = "select o.orders_id, o.delivery_postcode from " . TABLE_ORDERS . " o where o.orders_status = 2 and o.delivery_country = 'Canada' order by orders_id DESC"; $orders_query = tep_db_query($orders_query_raw); //$orders = tep_db_fetch_array($orders_query); while ($orders = tep_db_fetch_array($orders_query)) { print_r( $orders ); } //find matches between the imported csv array and the array of order postal codes
So I have the 2 arrays printing out:
From csv upload:
Array ( [0] => Array ( [0] => delivery_postcode [1] => tracking ) [1] => Array ( [0] => A0N 2H0 [1] => 3197653232325 ) [2] => Array ( [0] => A3N 3A6 [1] => 31976532323252 ) )
From sql selection:
Array ( [orders_id] => 32061 [delivery_postcode] => V0R 2M0 ) Array ( [orders_id] => 32011 [delivery_postcode] => L8B1H5 ) Array ( [orders_id] => 31846 [delivery_postcode] => K0E1K0 ) Array ( [orders_id] => 31822 [delivery_postcode] => A0N 2H0 ) Array ( [orders_id] => 31503 [delivery_postcode] => V0J 1Z2 )
What I want to do is compare the postal codes from the csv with the postal codes from the sql selection. And then if a match is found select both the order_id and the tracking_number and push those to a new array together.
I am not sure about a few things:
- my csv file open seems to be a different amount of dimensions to the array of my selection so I am not sure if that will matter but I think it will
- I am not sure what to do to compare the postal codes. I think I will ned to use a foreach loop to check for matches but I am little shaky on this because they are different dimensions.
- My postal codes have different formatting, some with spaces and some without so I think I will have to address that by converting the formatting to be consistent.
Advertisement
Answer
You can achieve this result by using array_search
to find the delivery_postcode
for each order in $csv_array
, and if found, merging the entry from $csv_array
with the entry from $orders
to make the output tracking array. Note I have re-indexed $csv_array
with the keys from the first line of the CSV file, and I am removing all spaces from the postcode values (so that e.g. A0N2H0
will match A0N 2H0
) to make this possible.
$csv_keys = array_shift($csv_array); $csv_array = array_map(function ($a) use ($csv_keys) { return array_combine($csv_keys, array(str_replace(' ', '', $a[0]), $a[1])); }, $csv_array); $tracked = array(); foreach ($orders as $order) { if (($key = array_search(str_replace(' ', '', $order['delivery_postcode']), array_column($csv_array, 'delivery_postcode'))) !== false) { $tracked[] = array_merge($order, $csv_array[$key]); } } print_r($tracked);
Output (for your sample data):
Array ( [0] => Array ( [orders_id] => 31822 [delivery_postcode] => A0N2H0 [tracking] => 3197653232325 ) )