In PHP, convert a PHP object to an associative array. A simple PHP array is a data structure that keeps one or more comparable sorts of information in a single name, while an associative array is not.
Array is the data structure that stores one or more similar type of values in a single name but associative array is different from a simple PHP array.
An associative array is an array that contains the string index. Rather than saving element values in linear index order, this saves element values associated with key values.
Also Checkout other dynamic MySQL query tutorials,
We can convert object to associative array using json_encode() and json_decode() method. We can also convert using PHP Type Casting object to an array.
We can convert the PHP Object to an associative array using json_encode
and json_decode
methods.
The sample code:
class Employee { function __construct($name, $age) { $this->name = $name; $this->age = $age; } } // Creating the object $emp = new Employee('Parvez', 35); var_dump($emp); // Converting object to associative array $arr = json_decode(json_encode($emp), true); var_dump($arr);
Output:
object(Employee)#1 (2) { ["name"]=> string(6) "Parvez" ["age"]=> int(35) } array(2) { ["name"]=> string(6) "Parvez" ["age"]=> int(35) }
Typecasting is the process of converting one data type variable to another. It can, for example, use PHP’s typecasting rules to transform a PHP object into an array.
Syntax:
$arr = (array) $obj;
The sample code:
class Employee { function __construct($name, $age) { $this->name = $name; $this->age = $age; } } // Creating the object $emp = new Employee('Parvez', 35); var_dump($emp); // Converting object to associative array $arr = (array)$emp; var_dump($arr);
Output:
object(Employee)#1 (2) { ["name"]=> string(6) "Parvez" ["age"]=> int(35) } array(2) { ["name"]=> string(6) "Parvez" ["age"]=> int(35) }
The get_object_vars() is also used to convert object to array in PHP. The get object var() function retrieves the specified object’s properties and returns an associative array of those declared object properties.
get_object_vars(object)
class Employee { function __construct($name, $age) { $this->name = $name; $this->age = $age; } } // Creating the object $emp = new Employee('Parvez', 35); var_dump(get_object_vars($emp));
Output:
array(2) { ["name"]=> string(6) "Parvez" ["age"]=> int(35) }
This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More
in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More
This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More
We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More
in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More
I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More