PHP:list() 以字符串形式动态设置变量按格式化顺序读取任何 CSV 文件

问题描述

我可以将变量名称设置为 csv 标题(第一行)吗?

我确实这么认为,所以我编码:

$string = "date,name,age"; // csv file title order format
$tit = explode(",",$string); //array('date','name','age'); //will be called: $_date

//run PHP function to find a specific line
$arr = array('bash','monkey','badger'); //returned line
foreach ($tit as $i => $nam) { 
    $e = "_" . $nam; 
    $$e = $arr[$i];
} 
//set variables contents as title name
echo $_name; //monkey

完美运行:Try it

但是怎么把它变成PHP函数呢?

function toList ($tit,$arr) {
    foreach ($tit as $i => $nam) { 
        $e = "_" . $nam; 
        $$e = $arr[$i];
    }
}

$tit = array('date','age'); //will be called as: $_date
$arr = array('bash','badger');
    
toList($tit,$arr);
    
echo $_date;

作为函数代码中断。

应该如何将其编码为 PHP 函数

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)