How to display all objects in the array $ arrs. Through foreach. How to display $ arr or $ arr2 I know, but from the array how to get everything out.

 $ arrs = array ();
 $ arr = new stdClass ();
     $ arr-> name = "Andrii";
     $ arr-> age = "25";
     $ arr-> long = "1.85";
     $ arr-> male = "man";
 $ arr2 = new stdClass ();
     $ arr2-> name = "One";
     $ arr2-> age = "23";
     $ arr2-> long = "1.70";
     $ arr2-> male = "man";

 $ arrs [] = $ arr;
 $ arrs [] = $ arr2;

Understood. It was necessary just 2 foreach

 foreach ($ arrs as $ key => $ value) {
     foreach ($ value as $ key1 => $ value1) {
        echo $ key1. "=>". $ value1;
     }
 }
  • What do you mean by the notion of outputting array objects? - Yaroslav Molchan
  • I have in the array $ arrs two objects $ arr and $ arr2. How to output them in a loop from the $ arrs array? - Andrii

1 answer 1

You can display all the fields of an object, for example, like this:

$obj = new \stdClass(); $obj->foo = 'bar'; $obj->baz = 'quux'; foreach (get_object_vars($obj) as $key => $value) { echo($key . ' => ' . $value . "\n"); } 

Displays:

 foo => bar baz => quux 

And here is a working example at 3v4l.org.

To iterate over an array of objects, you can use another (external) foreach .

  • This is the output from the object. And I have objects in the array. I know that I can output this way: Or it’s necessary to output objects from an array. foreach ($ arr2 as $ key => $ value) {echo $ key. '' -> ". $ value;} - Andrii
  • one
    Add an external foreach for the array and you will be "happy" :) - Dmitriy Simushev