load a csv string into mysql
Moderator: General Moderators
load a csv string into mysql
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
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
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
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 :
This is what i get back:
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);
?>
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
What are you trying to get? Something like:
Name->first, John Doe->John, Billy Bob->Billy?
Name->first, John Doe->John, Billy Bob->Billy?
Re: load a csv string into mysql
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
but i got it figured out thanks though