基本的なスクリプトの一部の(単純なはずの)PHPコードに奇妙な問題が発生しています。
var_dumpを使用すると、次のような多次元の連想配列$ accountsがあります。
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;
}
4つのアカウント文字列が出力されると思います。代わりに、$ 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ファイルを解析した後、データをフィルタリングしてください。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加