Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iterating through a stdClass object in PHP

Tags:

I have an object like this:

stdClass Object (     [_count] => 10     [_start] => 0     [_total] => 37     [values] => Array         (             [0] => stdClass Object                 (                     [_key] => 50180                     [group] => stdClass Object                         (                             [id] => 50180                             [name] => CriticalChain                         )                  )              [1] => stdClass Object                 (                     [_key] => 2357895                     [group] => stdClass Object                         (                             [id] => 2357895                             [name] => Data Modeling                         )                  )              [2] => stdClass Object                 (                     [_key] => 1992105                     [group] => stdClass Object                         (                             [id] => 1992105                             [name] => SQL Server Users in Israel                         )                  )              [3] => stdClass Object                 (                     [_key] => 37988                     [group] => stdClass Object                         (                             [id] => 37988                             [name] => CDO/CIO/CTO Leadership Council                         )                  )              [4] => stdClass Object                 (                     [_key] => 4024801                     [group] => stdClass Object                         (                             [id] => 4024801                             [name] => BiT-HR, BI & IT Placement Agency                         )                  )              [5] => stdClass Object                 (                     [_key] => 37845                     [group] => stdClass Object                         (                             [id] => 37845                             [name] => Israel Technology Group                         )                  )              [6] => stdClass Object                 (                     [_key] => 51464                     [group] => stdClass Object                         (                             [id] => 51464                             [name] => Israel DBA's                         )                  )              [7] => stdClass Object                 (                     [_key] => 66097                     [group] => stdClass Object                         (                             [id] => 66097                             [name] => SQLDBA                         )                  )              [8] => stdClass Object                 (                     [_key] => 4462353                     [group] => stdClass Object                         (                             [id] => 4462353                             [name] => Israel High-Tech Group                         )                  )              [9] => stdClass Object                 (                     [_key] => 4203807                     [group] => stdClass Object                         (                             [id] => 4203807                             [name] => Microsoft Team Foundation Server                         )                  )          )  ) 

I need to get the id and name in an HTML table, but I seem to have a hard time iterating through this object. TIA. I understand that I need to get to the Values Array, and then to the group object, but I trip over the transitions between object and array and foreach vs index based iteration.

For example I tried this:

foreach ($res as $values) { print "\n"; print_r ($values); }  

It iterates trough the object, but it also gives me useless

10 0 37 
like image 298
Mordechai Avatar asked Apr 29 '13 20:04

Mordechai


People also ask

How to iterate object array in PHP?

Use the foreach($array_name as $element) to iterate over elements of an indexed array. Use the foreach($array_name as $key => $value) to iterate over elements of an associative array.

How do you access the properties of an object in PHP?

Within class methods non-static properties may be accessed by using -> (Object Operator): $this->property (where property is the name of the property). Static properties are accessed by using the :: (Double Colon): self::$property .

How to iterate through an object in PHP?

PHP provides a way for objects to be defined so it is possible to iterate through a list of items, with, for example a foreach statement. By default, all visible properties will be used for the iteration. echo "\n"; $class->iterateVisible();

What is stdClass object in PHP?

The stdClass is the empty class in PHP which is used to cast other types to object. It is similar to Java or Python object. The stdClass is not the base class of the objects. If an object is converted to object, it is not modified.


2 Answers

echo "<table>"  foreach ($object->values as $arr) {     foreach ($arr as $obj) {         $id   = $obj->group->id;         $name = $obj->group->name;          $html  = "<tr>";         $html .=    "<td>Name : $name</td>";         $html .=    "<td>Id   : $id</td>";         $html .= "</tr>";     } }  echo "</table>"; 
like image 90
adeneo Avatar answered Sep 19 '22 07:09

adeneo


Since this is the top result in Google if you search for iterate over stdclass it may be helpful to answer the question in the title:

You can iterate overa stdclass simply by using foreach:

$user = new \stdClass(); $user->flag = 'red';  foreach ($user as $key => $value) {    // $key is `flag`    // $value is `red` } 
like image 34
Adam Avatar answered Sep 21 '22 07:09

Adam