5 Replies - 414 Views - Last Post: 20 November 2017 - 10:06 AM Rate Topic: -----

#1 DB9323   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-November 17

PHP Load a csv file into an array, pick out all unique items in column

Posted 18 November 2017 - 01:57 PM

Hello,

I need to download a csv from the city of chicago containing a list of all towed cars. There is a 'Make' column containing the make of each car towed. What I need to do is gather all of the unique makes from that c\olumn in the csv.

Here is my attempt at a solution:

$towFileString = file_get_contents("the link would go here");
file_put_contents('tow-data.csv', $towFileString);

$csvArray = array_map('str_getcsv', file('tow-data.csv'));

//echo count($csvArray);

$allMakes = array_column($csvArray, 'Make');
$uniqueMakes = array_unique($allMakes);

echo count($uniqueMakes);


I can see that an array is being created and populated ($csvArray) because when I echo the count there are 5000+ items (the city needs money :) But when I attempt to create the array $uniqueMakes and count it, I get 0. However, upon examining the csv, I can see there are indeed plenty of Makes.

So where did I stray here?

Thank you for your help,
DB

Is This A Good Question/Topic? 0
  • +

Replies To: PHP Load a csv file into an array, pick out all unique items in column

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5241
  • View blog
  • Posts: 14,037
  • Joined: 18-April 07

Re: PHP Load a csv file into an array, pick out all unique items in column

Posted 18 November 2017 - 04:20 PM

When you print_r($csvArray) what do you see? Can you show us some of the data stored in $csvArray?
Was This Post Helpful? 0
  • +
  • -

#3 DB9323   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-November 17

Re: PHP Load a csv file into an array, pick out all unique items in column

Posted 18 November 2017 - 04:48 PM

View PostMartyr2, on 18 November 2017 - 04:20 PM, said:

When you print_r($csvArray) what do you see? Can you show us some of the data stored in $csvArray?


Sure, I should have included that with my question. Here is a sample csv. The actual one is much longer.

Tow Date,Make,Style,Model,Color,Plate,State,Towed to Address,Tow Facility Phone,Inventory Number
09/03/2017,PONT,4D,,SIL,AH28462,IL,701 N. Sacramento,(773) 555-7605,6895835
11/01/2017,DODG,VN,,PLE,E991126,IL,701 N. Sacramento,(773) 555-7605,6901232
11/01/2017,HOND,LL,,BLK,AG26615,IL,701 N. Sacramento,(773) 555-7605,6901231
11/01/2017,HOND,2H,,BLK,E742368,IL,10300 S. Doty,(773) 555-8495,2834773
11/01/2017,PONT,4D,,GRY,,IL,10300 S. Doty,(773) 555-8495,2834772
10/04/2017,AUDI,4D,,BLU,888044,RI,701 N. Sacramento,(773) 555-7605,6898741



When I use print_r($svArray) I get data like:
Array ( [0] => Array ( [0] => Tow Date [1] => Make [2] => Style [3] => Model [4] => Color [5] => Plate [6] => State [7] => Towed to Address [8] => Tow Facility Phone [9] => Inventory Number )



Thank you for helping me out,
DB

Actually this more copmplete output from print_r

Array ( [0] => Array ( [0] => Tow Date [1] => Make [2] => Style [3] => Model [4] => Color [5] => Plate [6] => State [7] => Towed to Address [8] => Tow Facility Phone [9] => Inventory Number ) [1] => Array ( [0] => 09/03/2017 [1] => PONT [2] => 4D [3] => [4] => SIL [5] => AH28462 [6] => IL [7] => 701 N. Sacramento [8] => (888) 555-7605 [9] => 6895835 ) [2] => Array ( [0] => 11/01/2017 [1] => DODG [2] => VN [3] => [4] => PLE [5] => E991126 [6] => IL [7] => 701 N. Sacramento [8] => (888) 555-7605 [9] => 6901232 ) 


This post has been edited by astonecipher: 18 November 2017 - 06:03 PM
Reason for edit:: Removed phone numbers

Was This Post Helpful? 0
  • +
  • -

#4 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2563
  • View blog
  • Posts: 10,281
  • Joined: 03-December 12

Re: PHP Load a csv file into an array, pick out all unique items in column

Posted 18 November 2017 - 06:05 PM

The issue is, you are only mapping the single row.

Your options are, use the index of the column, or add additional overhead and loop through the file to manually map the indexed column to the named column.
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3796
  • View blog
  • Posts: 13,742
  • Joined: 08-August 08

Re: PHP Load a csv file into an array, pick out all unique items in column

Posted 19 November 2017 - 07:18 AM

$towFileString = file_get_contents("the link would go here");
file_put_contents('tow-data.csv', $towFileString);

$csvArray = array_map('str_getcsv', file('tow-data.csv'));


This is repetitive. Why load the file into a variable, store the variable in another file, then load that new file into a new variable? Why not use file on the original file?
Was This Post Helpful? 0
  • +
  • -

#6 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2041
  • View blog
  • Posts: 6,264
  • Joined: 15-January 14

Re: PHP Load a csv file into an array, pick out all unique items in column

Posted 20 November 2017 - 10:06 AM

array_column isn't going to return anything when you tell it you want the column "Make", because the array does not have a key called "Make". Look at the output from print_r, use the numerical index for the column.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1