This site is mobile accessible. Press the "Tap Here" button to use a different font-size.


Smartphone icons created by Freepik - Flaticon

PHP Casting

Cast to Object


object(stdClass)#1 (3) {
  ["0"]=>
  string(5) "Volvo"
  ["1"]=>
  string(3) "BMW"
  ["2"]=>
  string(6) "Toyota"
}
object(stdClass)#2 (3) {
  ["Peter"]=>
  string(2) "35"
  ["Ben"]=>
  string(2) "37"
  ["Joe"]=>
  string(2) "43"
}

Indexed arrays converts into objects with the index number as property name and the value as property value.

Associative arrays converts into objects with the keys as property names and values as property values.

<?php
// arrays to be cast to objects
$a = array("Volvo", "BMW", "Toyota"); // indexed array
$b = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43"); // associative array

// casting the arrays to objects
$a = (object) $a;
$b = (object) $b;

// To verify the type of any object in PHP, use the var_dump() function:
var_dump($a);
var_dump($b);
?>