Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP7.1中从关联数组中读取值的奇怪问题_Php_Arrays_Csv - Fatal编程技术网

PHP7.1中从关联数组中读取值的奇怪问题

PHP7.1中从关联数组中读取值的奇怪问题,php,arrays,csv,Php,Arrays,Csv,我在基本脚本中看到一些(应该是简单的)PHP代码出现了一个奇怪的问题 我有一个多维关联数组$accounts,在使用var_dump时如下所示: Array(4) { [0] => array(3) { 'account' => string(37) "Flood Cleanup City - Desktop - Exact " 'parameter' => string(23)

我在基本脚本中看到一些(应该是简单的)PHP代码出现了一个奇怪的问题

我有一个多维关联数组$accounts,在使用var_dump时如下所示:

    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) ""}
}
所以,很简单。此数组在函数中生成,并作为返回值传递给变量$listAccounts

我只想迭代$listAccounts并提取'account'值,所以我写了以下内容:

foreach($listAccount as $account)
{
    $accountName = $account['account'];
    echo $accountName;
}
我希望它会输出四个帐户字符串。相反,$account['account']返回NULL。但是,如果我使用array_keys函数从数组中提取键的名称并使用此代码,它将正常工作:

$accountName = $account[array_keys($account)[0]];
在可能相关的情况下,生成多维数组的函数使用fgetcsv()函数解析CSV文件:

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;
}

在“帐户”密钥名称中有一个奇怪的第一个不可见符号。请在解析CSV文件后筛选数据。

该字符的十六进制代码为:
efbbbf
。。。这是BOM表(字节顺序标记)。如果可能的话,最好不用它来保存.csv。但是如果你不能,那么在解析之前把它从文件的前面拉下来(因为trim不容易去掉)。谢谢你们!就这样。我正在使用PhpStorm,并且能够直接在IDE内部使用CSV文件上的“删除BOM”选项。非常感谢。你可能不认为复制链接是相关的,但它也与toor逃避直接相关。该文件中有一个BOM表,该BOM表是通过
fgetcsv
中的第一个标题字段读取的。在进行csv解析之前,您需要先清除它。