user3314502
user3314502

Reputation: 21

php count filds of object

I have this array:

 0 => 
    object(stdClass)[31]
      public 'id_region' => string '1' (length=1)
      public 'nazwa' => string 'Bieliny' (length=7)
      public 'miejsc' => 
        object(stdClass)[16]
          public '0' => string 'Wilkw' (length=7)
          public '1' => string 'Zagnask' (length=9)
          public '2' => string 'gsdgf' (length=5)
          public '3' => string 'Zagnagfdfdsk' (length=14)
  1 => 
    object(stdClass)[32]
      public 'id_region' => string '2' (length=1)
      public 'nazwa' => string 'Bodzentyn' (length=9)
      public 'miejsc' => 
        object(stdClass)[51]
          public '0' => string 'Masw' (length=8)

I know how display this data, but I dont`t know how count the fields od second object (object(stdClass)[16]). I try normal:

foreach($query as $dane) { 
     count($dane->miejsc);
}

But this always return 1.

Upvotes: 0

Views: 684

Answers (1)

pes502
pes502

Reputation: 1587

Try this:

foreach($query as $dane) { 
     count(get_object_vars($dane->miejsc));
}

This uses PHP's get_object_vars function, which will return the properties of an object as an array. You can then use this array with PHP's count function.

Upvotes: 1

Related Questions