3v4l.org

run code in 300+ PHP versions simultaneously
<?php class SomeClass { public $array; public $content; public $item; public function someMethod() { $this->array = [ "testing" => "testing one two three", "apples" => "red", "oranges" => "orange", ]; foreach($this->array as $this->item) { // I pass $this->item down inside the loop echo $this->content; // this here is looped } } } $class = new SomeClass(); /** * I want to access $this->item without having to do a loop * * I don't want this e.g: foreach($this->array as $this->item) { * echo $this->item; * } * * but instead I just want to do this: foreach($this->array as $this->item) { * echo $this->content; this here will output the content * } * * and inside `$class->content` it has the variable that is passed down: `$this->item`. * that way I don't HAVE to write a loop and all I have to do is write `$this->content`. * that means, I don't want to write a loop! I just want to write what the loop outputs! * and I'm having troubles getting the item that is passed inside of the loop. */ // I want to do this but I can't figure out how to access $class->item outside of loop $class->content = $class->item; // I could do something that works: foreach($class->array as $class->item) { echo $class->item; } // but I want to access `$class->item` outside of the loop and use it in: $class->content = $class->item; // then call the method that will loop and echo the property `content` $class->someMethod();

This is an error 500

Value for `_results` contains invalid data `array`


preferences:
174.37 ms | 2350 KiB | 8 Q