I have a problem with associative-array in PHP – when the source of the arrays is from a text file.
When I write something as follows:
$logins = array('user1' => '1234','user2' => '2345','user3' => '3456');
It all works as expected.
So, I tried to call those arrays from CSV file like that:
$file_handle = fopen("data.csv", "r");
while (!feof($file_handle) ) {
$line_of_text = fgetcsv($file_handle, 1024);
if (empty($line_of_text)) { break; }
$logins = array($line_of_text[0] . '=>' . $line_of_text[1]); /* remove the => and seperate the logins with "," on CSV */
}
It didn't work.
There are a lot close related questions and answers here on SO but I did read and try to implant them without no success. Please Guide me.
Edit: data.csv looks like as follows.
user1,1234;
user2,2345;
user3,3456;
data.txtthat you've shared above is a string representation of an array, then that will never work (note that you don't actually opendata.txtin your code, you opendata.csv)$logins = array($line_of_text[0] . '=>' . $line_of_text[1]);to$logins[$line_of_text[0]] = $line_of_text[1];