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 Array


array(2) {
  ["color"]=>
  string(3) "red"
  ["model"]=>
  string(5) "Volvo"
}

Objects converts into associative arrays where the property names becomes the keys and the property values becomes the values.

<?php
// declare class Car
class Car {
  public $color;
  public $model;
  public function __construct($color, $model) {
    $this->color = $color;
    $this->model = $model;
  }
  public function message() {
    return "My car is a " . $this->color . " " . $this->model . "!";
  }
}

// declare $myCar object
$myCar = new Car("red", "Volvo");

// cast $myCar object to an associative array
$myCar = (array) $myCar;
var_dump($myCar);
?>