Page 1 of 1

load a csv string into mysql

Posted: Sat May 17, 2008 11:20 pm
by corbo950
i have some csv data that is in a string the first line has all the colum names and i need 2 know how to put that data into an assocative array based on the column names please help and no the csv is not in a file

Re: load a csv string into mysql

Posted: Mon May 19, 2008 3:43 pm
by Jade
Sounds to me like you'd need to write something that breaks up the string and dumps it into either a SQL query or into a file that you can then import into mysql. If that's true then you'd need to

1) Extract all the column names (how do you know where the columns start and the data ends? that's your delimiter)

2) Using your delimiter put in an insert statement using the column names and the remaining data

3) Dump it into a file or an SQL statement

4) Import the file or execute the SQL

Re: load a csv string into mysql

Posted: Mon May 19, 2008 6:51 pm
by corbo950
ya i got something to extract the data now i just need a function that will replace the keys in an array with an array of new keys:

This is what i have come up with so far :

Code: Select all

<?php
 
    $lines_array = array(
    array("Name", "first"),
    array("John Doe", "John"),
    array("Billy Bob", "Bill")
    );
    $new_lines = array();
    while(list($pos, $line_array) = each($lines_array))
    {
        if($pos == 0)
        {
            $new_keys = $line_array;
        }
        else
        {
            $new_info = $line_array;
            array_push($new_lines, $new_info);
        }
    }
    $count = count($new_keys);
    $new_array = array();
    $new_arrays = array();
    $num = 0;
    while(list($pos, $new_line) = each($new_lines))
    {
        while(list($pos, $attribute) = each($new_line))
        {
            $new_key = $new_keys[$pos];
            $new_array[$new_key] = $attribute;
            array_push($new_arrays, $new_array);
        }
    }
    print_r($new_array);
?>
 
This is what i get back:
Array ( [Name] => John Doe ) Array ( [Name] => John Doe [first] => John ) Array ( [Name] => Billy Bob [first] => John ) Array ( [Name] => Billy Bob [first] => Bill ) Array ( [Name] => Billy Bob [first] => Bill )

Re: load a csv string into mysql

Posted: Tue May 20, 2008 3:51 pm
by Jade
What are you trying to get? Something like:

Name->first, John Doe->John, Billy Bob->Billy?

Re: load a csv string into mysql

Posted: Thu May 22, 2008 1:52 am
by corbo950
no replace the keys array([0]=>array([name]=>john doe, [first]=>john)[2]=>array([name]=>Billy bob, [first]=>bill)

but i got it figured out thanks though