0

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
3
  • 2
    What does that print_r prints ? It would be damn useful to know that ! Commented Aug 4, 2017 at 15:46
  • And code can be condensed to sandbox.onlinephpfunctions.com/code/… Commented Aug 4, 2017 at 16:01
  • echo "Name: ".gettype($returnVal[0]['Name'])."<br>"; Echo's "NULL" if that helps. Not sure why its null when a print_r and Var_dump prints it out Commented Aug 4, 2017 at 16:57

2 Answers 2

1

so, it seems like

 reset($returnVal[0]);

is the answer to my issue, the reset function gave me my value for the array. But I would still like to know why the normal access to the element was not working.

Sign up to request clarification or add additional context in comments.

Comments

1

You say in a comment rmove double quotes from array key but you are not actually doing that. That code doesn't work. You can see in an example output of var_dump that keys are not quoted, but in your sample output they are.

fgetcsv should remove enclosed quotes, which means either your csv files are double quoted (which may be correct) or something else is going on. You can access your property with $returnVal[0]['"Name"']

To actually remove keys you should probably foreach the header row and set str_replace or trim each key manually and set it to the $header variable. Something like this:

$header = array();
foreach($row as $key => $value) {
    $key = trim($key, "\" \t\n\r\0\x0B"); // trim quotes as well as standard trim characters
    $header[$key] = $value;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.