www.webdeveloper.com
Results 1 to 3 of 3

Thread: CSV to PHP array

Hybrid View

  1. #1
    Join Date
    Jul 2009
    Location
    UK
    Posts
    174

    CSV to PHP array

    hi

    I need to parse and convert some .csv file data to PHP array. My data might look like:

    Code:
    name,address_one,address_two,address_postcode
    Bob,1 A Street,A town,AA11AA
    Andy,92 Church St,Manchester,M20 3JN
    Sarah,893 London Road,Enfield,EN3 9HB
    Freda,67 Green Close,Newcastle,Nw5 2ED
    Some of the headers have underscores in them. I need to split these such that the split string would be a sub element of the original string. E.g.

    Taking the header 'address_one', then split it on '_' and we'd have 'one' as a sub-element of address. I want to loop through the entire contents of my data file with my array eventually looking like:

    PHP Code:
    Array
    (
        [
    'name']=> Bob
        
    [0] => Array
            (
                [
    address] => Array
                    (
                        [
    one] => 1 A Street
                        
    [two] => A town
                        
    [postcode] => WC2 9GH
                                                   
                    
    )
            )
        [
    'name']=> Andy
        
    [1] => Array
            (
                [
    address] => Array
                    (
                        [
    one] => 92 Church St
                        
    [two] => Manchester
                        
    [postcode] => M20 3JN
                                                   
                    
    )
            )
    ...
    omitted   

    So headers are the keys/sub keys and the data are the array values.



    What I've tried thus far:

    PHP Code:
    public function parseFile($fileIn)
        {
            
            if ((
    $handle fopen("$fileIn""r")) !== FALSE
            {
                    
                
    $lines file("$fileIn");
                
    $headers explode','"$lines[0]");

                foreach(
    $headers as $header)
                {            
                    if(
    $x strpos("$header"'_')){
                        
                        
    $child trim(substr("$header"$x), '_');
                        
    $parent substr("$header"0$x);

                        print 
    "parent: $child\n";
                    } 
                
                }
        
                    
    fclose($handle);
            } else {
                throw new 
    Exception('Unable to open the file!');
            }
        } 
    // parseFile 
    I figure I'm going to need some temporary arrays combined with PHP array functions. I've looked at some of the usual suspects: array_push, array_walk, array_map......etc



    Best or easiest way to get the data into this format? Clues, suggestions...

    thanks in advance.
    Success is the ablility to go from one failure to another without loss of enthusiasm.
    -- Sir Winston Churchill.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,921
    Untested:
    PHP Code:
        public function parseFile($fileIn)
        {
            
    $data = array();
            if ((
    $handle fopen($fileIn"r")) !== FALSE) {
                while(
    $line fgetcsv($handle)) {
                    
    $data[$line[0]][] = array(
                        
    'address' => array(
                            
    'one'      => $line[1],
                            
    'two'      => $line[2],
                            
    'postcode' => $line[3]
                        )
                    );
                }
            }
            else {
                throw new 
    Exception("Oops!");
            }
            return 
    $data;
        } 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Jul 2009
    Location
    UK
    Posts
    174
    Quote Originally Posted by NogDog View Post
    Untested:
    PHP Code:
        public function parseFile($fileIn)
        {
            
    $data = array();
            if ((
    $handle fopen($fileIn"r")) !== FALSE) {
                while(
    $line fgetcsv($handle)) {
                    
    $data[$line[0]][] = array(
                        
    'address' => array(
                            
    'one'      => $line[1],
                            
    'two'      => $line[2],
                            
    'postcode' => $line[3]
                        )
                    );
                }
            }
            else {
                throw new 
    Exception("Oops!");
            }
            return 
    $data;
        } 
    Thanks NogDog, I was struggling a little there.
    Success is the ablility to go from one failure to another without loss of enthusiasm.
    -- Sir Winston Churchill.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles