我真的是PHP新手,所以请原谅我愚蠢的问题。
为什么我的循环不起作用,谢谢?
PHP Warning: Invalid argument supplied for foreach() in /Users/user/Desktop/csv_transformation.php on line 16警告:第16行的/Users/user/Desktop/csv_transformation.php ation.php中为foreach()提供的参数无效
class Processor
{
public $arr1;
function __construct($arr1)
{
$this->arr1 = $arr1;
}
function get_arr()
{
foreach ($this->arr1 as $element) {
echo "array 1 = " . $element . PHP_EOL;
};
}
}
$test = new Processor("one", "two", "three");
$test->get_arr();发布于 2020-09-22 19:52:28
你有没有尝试过给你的函数(在[]中)代码块提供一个数组?
$test = new Processor(["one", "two", "three"]);
发布于 2020-09-22 20:37:30
您可以传递多个参数,而不是单个参数,因为您没有使用[]包装。这意味着您正在传递三个单独的字符串。在类内部,你正在初始化一个字符串。对于foreach循环,你给出的字符串是不可能的。因为foreach只接受对象或数组。因此传递数组而不是多个字符串
$test = new Processor(["one", "two", "three"]);https://stackoverflow.com/questions/64009131
复制相似问题