I have the below code and once I pulled in a CSV file. That one key in the array had double quotes around it. I was not sure what to do so I stripped them out thinking this would allow me to simply put $array[0]['Name'] to access the string. But No! nothing. It just keeps coming up blank.
// get file and open it into a variable
$file = fopen('my.csv', 'r') or die('Unable to open file!');
// set array to return data in
$returnVal = array();
// set header variable
$header = null;
// loop through data
while(($row = fgetcsv($file)) !== false){
// make the header array (key)
if($header === null){
echo "<pre>";
//print_r($row);
$row[0] = trim(str_replace('"', '', $row[0])); // rmove double quotes from array key
//echo "<br>".$row[0];
print_r($row);
$header = $row;
continue;
} // end of set header
//get just names and echo them
//echo "<br>".$row[0];
// more row by row column by column and set data to correct header
$newRow = array();
// loop rows and set data
for($i = 0; $i<count($row); $i++){
$newRow[$header[$i]] = $row[$i];
//echo "<br>" . $newRow[$header[$i]];
//$newRow['First_Name'] = $row[$i];
//unset($returnVal['"Name"']);
}// end for loop
$returnVal[] = $newRow;
}// end while loop
// close csv file
fclose($file);
// $returnVal now contains the contents of the CSV file
echo "Name: ".$returnVal[0]['Name'] . "<br>";
echo "Email: ".$returnVal[0]['Email Address'];
echo "<pre>";
print_r($returnVal);
// echo $returnVal[0]["Name"];
//var_dump($returnVal);
***************** EDIT **************************
sample output of var_dump (print_r has same output)
array(13500) {
[0]=>
array(5) {
["Name"]=>
string(10) "my name"
["Email Address"]=>
string(19) "[email protected]"
["Date Added"]=>
string(19) "2017-03-27 03:38 PM"
["Signup Date"]=>
string(10) "2016-04-04"
["Username"]=>
string(27) "myusername1459752576"
}
echo "Name: ".$returnVal[0]['Name'] . "<br>"; // prints nothing
echo "Email: ".$returnVal[0]['Email Address']; // prints email just fine