assigning values to variables within foreach loop
Posted
by Patrick
on Stack Overflow
See other posts from Stack Overflow
or by Patrick
Published on 2010-04-06T00:07:53Z
Indexed on
2010/04/06
0:13 UTC
Read the original article
Hit count: 160
php
Im working on a script to tear into a csv file and ive got the rows separated, but im not sure how to assign variables to the fields within the row, so that i can perform functions with them.
my code:
<?
ini_set('auto_detect_line_endings', true);
$csvraw = file_get_contents("file.csv");
$csv = explode("\n", $csvraw);
$i=0;
foreach($csv AS $key=>$value){
$rowsplit = explode(",", $value);
// This is where i get my headers
if($i == 0){
$col0 = $rowsplit[0];
$col1 = $rowsplit[1];
$col2 = $rowsplit[2];
$col3 = $rowsplit[3];
$col4 = $rowsplit[4];
$col5 = $rowsplit[5];
$col6 = $rowsplit[6];
$col7 = $rowsplit[7];
}
$i++;
//This is where i loop through each row's fields
foreach($rowsplit AS $key2=>$value2){
if(empty($value2)){
echo "";
}else{
echo "$value2 <br>";
}
// This is where i need to assign $variable0 through $variable7 so i can perform a function with the field values.
}
echo "<br><br><br>";
}
?>
© Stack Overflow or respective owner