Location: PHPKode > scripts > Array to Object converter > array-to-object-converter/example.php
<?php
     include('toobj.php'); // Including class' file
     
     $arrNum = array('first value', 'second value', 3, 4); // New numeric array
     $objNum = new toObj($arrNum); // Transporting a numeric array to object

     // Listing numeric by loop
     echo '<b>Values:</b><br />';
     for ($i = 0; $i < $objNum->varCount(); $i++)
     {
          echo $i . ': <i>' . $objNum->$i . '</i><br />';
     }
     // Showing each value
     echo 'Each: <i>' . $objNum->{1} . '</i><br /><br />'; // will show: second value
     
     // New assoc array
     $arrAssoc = array(
          'me' => 'Sebastian',
          'you' => 'Anonymous',
          'he' => 'Bill'
     );
     $objAssoc = new toObj($arrAssoc); // Transporting an assoc array to object
     
     // Listing assoc by loop
     echo '<b>Values:</b><br />';
     foreach ($objAssoc as $key => $value)
     {
          echo $key . ': <i>' . $value . '</i><br />';
     }
     // Showing each value
     echo 'Each: <i>' . $objAssoc->you . '</i><br /><br />'; // will show: Anonymous
     
     // New multivariate array
     $arrMulti = array(
          'me' => 'Sebastian',
          'car' => array('mercedes', 'lexus'),
          'motorbike' => array(
               'old' => 'yamaha',
               'new' => 'suzuki'
          )
     );
     $objMulti = new toObj($arrMulti);
     
     // Listing multivariate by recursive function
     function listMulti($object)
     {
          foreach ($object as $key => $value)
          {
               if (is_a($object->$key, 'toObj')) listMulti($object->$key);
               else echo $key . ': <i>' . $value . '</i><br />';
          }
     }
     echo '<b>Values:</b><br />';
     listMulti($objMulti);
     // Showing each value
     echo 'Each 1: <i>' . $objMulti->motorbike->old . '</i><br />'; // will show: yamaha
     echo 'Each 2: <i>' . $objMulti->car->{1} . '</i><br /><br />'; // will show: lexus
?>
Return current item: Array to Object converter