Convert an object to associative array in PHP Last Updated : 04 Jul, 2024 Comments Improve Suggest changes Like Article Like Report An object is an instance of a class. It is simply a specimen of a class and has memory allocated. The array is the data structure that stores one or more similar types of values in a single name but an associative array is different from a simple PHP array. An array that contains a string index is called an associative array. It stores element values in association with key values rather than in linear index order.Here we have some common methodsTable of ContentUsing json_decode and json_encode methodType Casting object to an arrayUsing get_object_vars() FunctionMethod 1: Using json_decode and json_encode methodThe json_decode function accepts JSON encoded string and converts it into a PHP variable on the other hand json_encode returns a JSON encoded string for a given value.Syntax: $myArray = json_decode(json_encode($object), true);Example: php <?php class sample { /* Member variables */ var $var1; var $var2; function __construct( $par1, $par2 ) { $this->var1 = $par1; $this->var2 = $par2; } } // Creating the object $myObj = new sample(1000, "second"); echo "Before conversion: \n"; var_dump($myObj); // Converting object to associative array $myArray = json_decode(json_encode($myObj), true); echo "After conversion: \n"; var_dump($myArray); ?> OutputBefore conversion: object(sample)#1 (2) { ["var1"]=> int(1000) ["var2"]=> string(6) "second" } After conversion: array(2) { ["var1"]=> int(1000) ["var2"]=> string(6) "second" }Method 2: Type Casting object to an arrayTypecasting is the way to utilize one data type variable into the different data type and it is simply the explicit conversion of a data type. It can convert a PHP object to an array by using typecasting rules supported in PHP.Syntax: $myArray = (array) $myObj;Example: php <?php class bag { /* Member variables */ var $item1; var $item2; var $item3; function __construct( $par1, $par2, $par3) { $this->item1 = $par1; $this->item2 = $par2; $this->item3 = $par3; } } // Create myBag object $myBag = new bag("Mobile", "Charger", "Cable"); echo "Before conversion : \n"; var_dump($myBag); // Converting object to an array $myBagArray = (array)$myBag; echo "After conversion : \n"; var_dump($myBagArray); ?> OutputBefore conversion : object(bag)#1 (3) { ["item1"]=> string(6) "Mobile" ["item2"]=> string(7) "Charger" ["item3"]=> string(5) "Cable" } After conversion : array(3) { ["item1"]=> string(6) "Mobile" ["item2"]=> string(7) "Charger" ["item3"]=> string(5) "Cable" }Method 3: Using get_object_vars() FunctionThe get_object_vars() function returns an associative array containing the properties of the given object.Syntax:$myArray = get_object_vars($object);Example: PHP <?php class car { /* Member variables */ var $make; var $model; var $year; function __construct($make, $model, $year) { $this->make = $make; $this->model = $model; $this->year = $year; } } // Create car object $myCar = new car("Toyota", "Corolla", 2020); echo "Before conversion: \n"; var_dump($myCar); // Converting object to an associative array $myCarArray = get_object_vars($myCar); echo "After conversion: \n"; var_dump($myCarArray); ?> OutputBefore conversion: object(car)#1 (3) { ["make"]=> string(6) "Toyota" ["model"]=> string(7) "Corolla" ["year"]=> int(2020) } After conversion: array(3) { ["make"]=> string(6) "Toyota"...PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples. Comment More infoAdvertise with us Next Article Convert an object to associative array in PHP V vivekkothari Follow Improve Article Tags : Web Technologies PHP PHP Programs JSON Similar Reads How to Get All Keys of an Associative Array in PHP? Given an Associative array, the task is to get all the keys of the associative array in PHP. There are two approaches to get all keys from the associative array, these are - using array_keys(), and foreach loop. In this article, we will explore these approaches with examples.Table of ContentApproach 2 min read How to Get All Values from an Associative Array in PHP? Given an Associative Array, the task is to get all values from the associative array in PHP. To get all the values from an associative array, you can use various approaches such as foreach loop, array_values() function, and array_map() function. In this article, we will explore each approach with de 3 min read How to check an array is associative or sequential in PHP? In PHP there is no need to write the variable type before the variable because it is loosely-typed. It takes datatype from user defined values that are stored in it. Arrays in PHP is a type of data structure that allows to store multiple elements of similar data type under a single variable thereby 2 min read How to access an associative array by integer index in PHP? There are two types of arrays in PHP, indexed and associative arrays. In case of indexed array strict numeric indexing is followed but in case of associative array there are keys corresponding to each element. The elements of an associative array can only be accessed by the corresponding keys. As th 3 min read How to get numeric index of associative array in PHP? In PHP we can associate name/label with each array elements using => symbol. This is very helpful as it is easy to remember the element because each element is represented by the label rather than the index value. Using array_keys() function: The array_keys() function is an inbuilt function in PH 1 min read Convert a String into an Array of Characters in PHP Given a string, the task is to convert the string into an array of characters using PHP. Examples:Input: str = "GFG" Output: Array( [0] => G [1] => f [2] => G)Input: str = "Hello Geeks"Output: Array( [0] => H [1] => e [2] => l [3] => l [4] => o [5] => [6] => G [7] => 4 min read How to Create an Array of Given Size in PHP? This article will show you how to create an array of given size in PHP. PHP arrays are versatile data structures that can hold multiple values. Sometimes, it's necessary to create an array of a specific size, either filled with default values or left empty. Table of Content Using array_fill() Functi 3 min read How to create a copy of an object in PHP? An object copy is created by using the clone keyword (which calls the object's __clone() method if possible). An object's __clone() method cannot be called directly. When an object is cloned, PHP will perform a shallow copy of all of the object's properties. Any properties that are references to oth 2 min read How to convert an array into object using stdClass() in PHP? To convert an array into the object, stdClass() is used. The stdClass() is an empty class, which is used to cast other types to object. If an object is converted to object, its not modified. But, if object type is converted/type-casted an instance of stdClass is created, if it is not NULL. If it is 3 min read How to loop through an associative array and get the key in PHP? Associative arrays are used to store key-value pairs. For example, to store the marks of the different subject of a student in an array, a numerically indexed array would not be the best choice. Instead, we could use the respective subjectâs names as the keys in our associative array, and the value 3 min read Like