php loop object code example

Example 1: php loop through object

foreach ($obj as $key => $value) {
    echo "$key => $value\n";
}

Example 2: php loop object

<?php
class MyClass
{
    public $var1 = 'value 1';
    public $var2 = 'value 2';
    public $var3 = 'value 3';

    protected $protected = 'protected var';
    private   $private   = 'private var';

    function iterateVisible() {
       echo "MyClass::iterateVisible:\n";
       foreach ($this as $key => $value) {
           print "$key => $value\n";
       }
    }
}

$class = new MyClass();

foreach($class as $key => $value) {
    print "$key => $value\n";
}
echo "\n";


$class->iterateVisible();

?>

Example 3: loop through object php

<?php
class MyClass 
{
  public $var1 = 'valeur 1';
  public $var2 = 'valeur 2';
  public $var3 = 'valeur 3';

  protected $protected = 'variable protégée';
  private   $private   = 'variable privée';

  function iterateVisible() {
     echo "MyClass::iterateVisible:\n";
     foreach ($this as $key => $value) {
         print "$key => $value\n";
     }
  }
}

$class = new MyClass();

foreach($class as $key => $value) {
    print "$key => $value\n";
}
echo "\n";


$class->iterateVisible();

Example 4: php loop object keys

echo $obj->name;
echo $obj->age;

Example 5: php loop through object

foreach ($objects as $obj) {
   echo $obj->property;
}

Example 6: php loop through object

// Try edit message
const data = {
  message: 'Hello world',
}

$('#msg').html(data.message)

console.log(data)

Tags:

Php Example