0

I have a csv uploader i'm creating to push to an order api. Using phpSpreadsheets toArray method, i have an array like so:

Array
(
  [0] => Array
      (
        [0] => product_sku
        [1] => product_qty
        [2] => shipping_name
        [3] => shipping_address1
        [4] => shipping_address2
        [5] => shipping_city
        [6] => shipping_county
        [7] => shipping_postcode
        [8] => shipping_type
        [9] => customer_id
      )
  [1] => Array
      (
        [0] => test_sku_1
        [1] => 3
        [2] => Bruce Wayne
        [3] => The Manor
        [4] => Near Arkahm Asylumn
        [5] => Gotham
        [6] => Greater Gothan
        [7] => B17MAN
        [8] => 1
        [9] => 14994333
      )
  [2] => Array
      (
        [0] => test_sku_2
        [1] => 2
        [2] => Bruce Wayne
        [3] => The Manor
        [4] => Near Arkahm Asylumn
        [5] => Gotham
        [6] => Greater Gothan
        [7] => B17MAN
        [8] => 1
        [9] => 14994333
      )

  [3] => Array
      (
        [0] => test_sku_3
        [1] => 7
        [2] => Bruce Wayne
        [3] => The Manor
        [4] => Near Arkahm Asylumn
        [5] => Gotham
        [6] => Greater Gothan
        [7] => L17MA2
        [8] => 1
        [9] => 14994333
      )
)

Each order will be on a new line, however if a customer orders two different items, i need to group them together using their postcode. As such i was aiming to rearange into this format:

[orders] => Array(
  Array(

    [shipping_name] => "Bruce wayne",
    [customer_id] => 14994333,
    [address] => Array(
      [shipping_address1] => "The Manor",
      [shipping_address2] => "Near Arham Asylumn",
      [shipping_city] => "Gotham",
      [shipping_county] => "Greater Gotham",
      [shipping_postcode] => "B17MAN",
    )
    [products] => Array(
      Array(
        [sku] => "test_sku_1",
        [quantity] => 3
      ),
      Array(
        [sku] => "test_sku_2",
        [quantity] => "2"
      )
    ) 
  )
)

Once of the first problems i encountered was trying to match the postcodes. I managed to get a count using:

$getDuplicates = array_count_values(array_map(function($duplicates) {
       return $duplicates[7]; //Where 7 is the postcode
}, $rows));

This worked in counting what i needed correctly. However from there i'm hitting a brick wall. If i'm counting the duplicates, i need it to also make a note of the rows it's already gone through so they aren't pushed incorrectly into the new array i want to make.

Pseudo it should be:

for each rows as row{
  if row isn't set to be ignored{
    for each count of this postcode{
      array_push the product sku and qty
      mark these rows as rows to be ignored
    }
  }
}

Can anyone help me with this?

Dan Ruxton
  • 13
  • 9
  • So basically you need to group by customer id and postcode? – apokryfos Jun 13 '18 at 10:44
  • No, the uploader itself is going to be for dropshipping. The customer id will be the same throughout every line of this csv so when pushing to the api it knows what account to put the orders under. – Dan Ruxton Jun 13 '18 at 10:51

1 Answers1

0

So, long story short, i've been so caught up in this i completely missed the obvious. To solve this, i took the original array from phpspreadsheet and did an array_multisort:

foreach ($rows as $key => $row) {
  $postcodes[$key] = $row[7]; //the postcode key
}
array_multisort($postcodes, SORT_DESC, $rows;

I then just worked my way through the array and if the postcode changed, i'd create a new array, otherwise i'd just push straight into the correct array for the products.

I feel really stupid i didn't think of this before. Thank you to @apokryfos for trying to help!

Dan Ruxton
  • 13
  • 9