Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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){ 
        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

like image 966
Tom Avatar asked Feb 11 '26 20:02

Tom


1 Answers

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

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

should be

foreach($array as $key=>$value) 
like image 69
Rocket Hazmat Avatar answered Feb 14 '26 08:02

Rocket Hazmat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!