开发者

Foreach loop inside a php class?

开发者 https://www.devze.com 2023-02-28 14:21 出处:网络
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. All the function has to do is echo back the co

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. 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. 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){ 
 开发者_如何学C       echo $key . " : " . $value;
    } 
}

any advice why it gives me this error

Notice: Undefined variable: value in (my page) on line 117

Fatal error: Cannot access empty property in (my page) on line 117


You used the wrong type of arrow in the foreach loop.

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

should be

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

精彩评论

暂无评论...
验证码 换一张
取 消