I'm seeing a strange issue with some (should be simple) PHP code in a basic script.
I have a multidimensional associative array $accounts that looks like this when var_dump is used:
Array(4) {
[0] =>
array(3) {
'account' =>
string(37) "Flood Cleanup City - Desktop - Exact "
'parameter' =>
string(23) "flood_cleanup_city_d_em"
'phone' =>
string(0) ""
}
[1] =>
array(3) {
'account' =>
string(51) "Flood Cleanup City - Desktop - Exact Call Extension"
'parameter' =>
string(3) "N/A"
'phone' =>
string(0) ""
}
[2] =>
array(3) {
'account' =>
string(38) "Flood Cleanup City - Desktop - Phrase "
'parameter' =>
string(23) "flood_cleanup_city_d_pm"
'phone' =>
string(0) ""
}
[3] =>
array(3) {
'account' =>
string(52) "Flood Cleanup City - Desktop - Phrase Call Extension"
'parameter' =>
string(3) "N/A"
'phone' =>
string(0) ""}
}
So, straightforward enough. This array is generated in a function and passed as the return value to a variable $listAccounts.
I just want to iterate over $listAccounts and extract the 'account' value, so I wrote this:
foreach($listAccount as $account)
{
$accountName = $account['account'];
echo $accountName;
}
I'd expect it would output the four account strings. Instead, $account['account'] is returning NULL. BUT, if I use the array_keys function to extract the names of the keys from the array and use this code, it works properly:
$accountName = $account[array_keys($account)[0]];
In case it might be relevant, the function that generates the multidimensional array is using the fgetcsv() function to parse a CSV file:
function getAccounts()
{
$handle = fopen("water.csv","r");
$header = NULL;
$accounts = array();
$n = 0;
while (!feof($handle)) {
$account = fgetcsv($handle);
if(!$header)
{
$header = $account;
}
else
{
$accounts[] = array_combine($header,$account);
}
}
fclose($handle);
echo var_dump($accounts);
return $accounts;
}