I am now doing a simple thing, I am going to read from a CSV file, that column A contains code such as "EN", column B contains specific name "English"..etc., and I want to read them into an associate array.
My current doing is like this:
$handle = fopen("Languages.csv","r") or die("EPIC FAIL!");
$languageArray = array(
while (($row = fgetcsv($handle, 1000, ",")) !== FALSE)
{
$row[0] => $row[1],
}
)
But it actually complains about my syntax, so I am just wondering if there is a way to initialize my associate array by getting all the rows of my csv file, and put the first string(from column A) as the key, the second string(from column B)as the value?
Thanks.
Initialize it as empty first:
$languageArray = array();
Then populate it using a separate while loop like this:
while (($row = fgetcsv($handle, 1000, ",")) !== FALSE)
{
$languageArray[$row[0]] = $row[1];
}
PHP arrays/hashes have no fixed size and are thus mutable in all kinds of ways, so even if you initialize them as empty you can populate them however you want later.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With