arrays - PHP: list() set variables dynamically form string (read any CSV file as formatted order)
Get the solution ↓↓↓Can I set variables names as csv title (first line)?
I do think it, so I code this:
$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
Works perfectly: Try it
But how to turn it into PHP function?
function toList ($tit,$arr) {
foreach ($tit as $i => $nam) {
$e = "_" . $nam;
$$e = $arr[$i];
}
}
$tit = array('date', 'name', 'age'); //will be called as: $_date
$arr = array('bash', 'monkey', 'badger');
toList($tit,$arr);
echo $_date;
As function, code breaks.
How should code it to Php function works?
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: trying to access array offset on value of type bool
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.