PHP CSV String to Array

PHP CSV string to array

Do this:

$csvData = file_get_contents($fileName);
$lines = explode(PHP_EOL, $csvData);
$array = array();
foreach ($lines as $line) {
$array[] = str_getcsv($line);
}
print_r($array);

It will give you an output like this:

Array
(
[0] => Array
(
[0] => 12345
[1] => Computers
[2] => Acer
[3] => 4
[4] => Varta
[5] => 5.93
[6] => 1
[7] => 0.04
[8] => 27-05-2013
)

[1] => Array
(
[0] => 12346
[1] => Computers
[2] => Acer
[3] => 5
[4] => Decra
[5] => 5.94
[6] => 1
[7] => 0.04
[8] => 27-05-2013
)

)

I hope this can be of some help.

Split a comma-delimited string into an array?

Try explode:

$myString = "9,admin@example.com,8";
$myArray = explode(',', $myString);
print_r($myArray);

Output :

Array
(
[0] => 9
[1] => admin@example.com
[2] => 8
)

csv string to array in php

Try this:

    $lines = explode("\n", $url);

$head = str_getcsv(array_shift($lines),";");

$data = array();
foreach ($lines as $line) {
$tempArr = str_getcsv($line,";");
$tempArr[5] = explode(",",$tempArr[5]);
$data[] = array_combine($head, $tempArr);
}

PHP csv string parsing

"str_getcsv" only parse one row at a time:

You need to run it for each row:

$rows = explode("\n", $csv);
foreach($rows as $row){
print_r( str_getcsv( $row, "\t", "'") );
}

PHP CSV to associative array with top row as keys and following rows as values

this is wrong:

// original string
$OriginalString = "fname,lname,a,b,c,d,e,f"

your original string had newlines which are very important here, meaning your $OriginalString should be:

// original string
// original string
$OriginalString = "fname,lname
a,b
c,d
e,f";

from there you can explode the string by lines,

$lines = explode("\n",$originalString);

from there it gets kindof-complex, you can iterate the lines with foreach, keeping in mind that the first line gives you the keys, and once you parse out the keys, you can iterate the next lines fragments with another explode+foreach, like

$keys = array();
$parsed = array();
foreach($lines as $lineno => $line){
if($lineno === 0){
// the first line is special, it contains the keys
$keys = explode(",",$line);
continue;
}
$fragments = explode(",", $line);
$current=array();
foreach($fragments as $fragment_number => $value){
$current[ $keys[$fragment_number] ]=$value;
}
$parsed [] = $current;
}

which gives

[
{
"fname": "a",
"lname": "b"
},
{
"fname": "c",
"lname": "d"
},
{
"fname": "e",
"lname": "f"
}
]

How to create an array from a CSV file using PHP and the fgetcsv function

Like you said in your title, fgetcsv is the way to go. It's pretty darn easy to use.

$file = fopen('myCSVFile.csv', 'r');
while (($line = fgetcsv($file)) !== FALSE) {
//$line is an array of the csv elements
print_r($line);
}
fclose($file);

You'll want to put more error checking in there in case fopen() fails, but this works to read a CSV file line by line and parse the line into an array.

Convert comma separated string with value to nested array

  1. Pass in your dynamic number of strings to a loop.

  2. Parse the comma-separated values.

  3. Use the values to push data into your array structure.

Code: (Demo)

$strings = ["name,firstname,Bob", "name,lastname,Dylan"];
$result = [];
foreach ($strings as $csv) {
[$level1, $level2, $value] = str_getcsv($csv);
$result[$level1][$level2] = $value;
}
var_export($result);

Output:

array (
'name' =>
array (
'firstname' => 'Bob',
'lastname' => 'Dylan',
),
)

Or more succinctly, you can push directly from the array destructuring step to achieve the same result as above: (Demo)

$result = [];
foreach ($strings as $csv) {
[$level1, $level2, $result[$level1][$level2]] = str_getcsv($csv);
}
var_export($result);

Convert CSV string row with no delimiters into an array

If you're using a file to get the csv's you should be able to use fgetcsv for this read more here.

$filename = "";
if (($handle = fopen($filename, "r")) !== false) {
while (($line = fgetcsv($handle, 0, "\t")) !== false) {
// $data should be an array of all the items in here.
print_r($line);
}
}


Related Topics



Leave a reply



Submit