简体   繁体   English

一个php类里面的foreach循环?

[英]Foreach loop inside a php class?

I have a very simple task yet it keeps failing. 我有一个非常简单的任务,但它仍然失败。

I want to have a function inside a class and the param that you pass into the function will be an array. 我想在一个类中有一个函数,你传递给函数的param将是一个数组。 All the function has to do is echo back the contents of the array. 所有功能都要回显数组的内容。

Echoing out all the variables in for example the $_POST array is very easy to achieve with a foreach loop. 使用foreach循环很容易实现回调所有变量,例如$ _POST数组。 However, it does not work in my class. 但是,它在我的班级中不起作用。

This was a very generic explanation. 这是一个非常通用的解释。 I you need more details please ask. 我需要更多细节请问。

public function check_if_filled($array){ 
    foreach($array as $key->$value){ 
        echo $key . " : " . $value;
    } 
}

any advice why it gives me this error 任何建议为什么它给了我这个错误

Notice: Undefined variable: value in (my page) on line 117 注意:未定义的变量:第117行(我的页面)中的值

Fatal error: Cannot access empty property in (my page) on line 117 致命错误:无法访问第117行(我的页面)中的空属性

You used the wrong type of arrow in the foreach loop. 您在foreach循环中使用了错误类型的箭头。

foreach($array as $key->$value)

should be 应该

foreach($array as $key=>$value) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM