{"id":89038,"date":"2025-10-08T10:52:08","date_gmt":"2025-10-08T05:22:08","guid":{"rendered":"https:\/\/www.guvi.in\/blog\/?p=89038"},"modified":"2026-09-16T13:35:04","modified_gmt":"2026-09-16T08:05:04","slug":"convert-object-to-array-in-php","status":"publish","type":"post","link":"https:\/\/www.guvi.in\/blog\/convert-object-to-array-in-php\/","title":{"rendered":"How to Convert Object to Array in PHP (5 Easy Methods, 2026)"},"content":{"rendered":"\n<p>To convert object to array in PHP, you can use built-in functions like type casting, get_object_vars(), or json_decode() depending on how simple or nested your data is. Since each method behaves differently, the right choice depends on your specific use case.<\/p>\n\n\n\n<p>That&#8217;s exactly what this guide covers, 5 practical techniques with code examples, along with the common errors developers run into along the way.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>TL;DR Summary<\/strong><\/h2>\n\n\n\n<ul>\n<li>There are several ways to convert object to array in PHP, and the right one depends on how simple or nested your data is.<\/li>\n\n\n\n<li>Type casting <code>(array) $object<\/code> is the fastest option for simple, non-nested data.<\/li>\n\n\n\n<li><code>get_object_vars()<\/code> returns only public properties as an array, keeping private data protected.<\/li>\n\n\n\n<li><code>json_encode()<\/code> combined with <code>json_decode()<\/code> is the go-to method for deep or nested structures.<\/li>\n\n\n\n<li>A manual <code>foreach<\/code> loop gives you full control over keys and values while building the array yourself.<\/li>\n\n\n\n<li>A custom recursive function is the most reusable way to convert object to array, handling both simple and deeply nested objects automatically.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What Are Objects and Arrays in PHP?<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1200\" height=\"630\" src=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/Objects-and-Arrays-in-PHP-1200x630.webp\" alt=\"\" class=\"wp-image-134415\" srcset=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/Objects-and-Arrays-in-PHP-1200x630.webp 1200w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/Objects-and-Arrays-in-PHP-300x157.webp 300w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/Objects-and-Arrays-in-PHP-768x403.webp 768w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/Objects-and-Arrays-in-PHP-1536x806.webp 1536w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/Objects-and-Arrays-in-PHP-150x79.webp 150w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/Objects-and-Arrays-in-PHP.webp 2048w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" title=\"\"><\/figure>\n\n\n\n<p><strong>Objects: <\/strong><\/p>\n\n\n\n<p>In PHP, an object is a type of <a href=\"https:\/\/www.guvi.in\/blog\/what-are-data-structures-and-algorithms\/\" target=\"_blank\" rel=\"noreferrer noopener\">data structure<\/a> that represents an instance of a class. It allows programmers to categorize data variables (properties) and functions; as a result, modeling real-world entities becomes easier and more straightforward.&nbsp;<\/p>\n\n\n\n<p>Objects are generally considered an ideal choice for organizing complex datasets because, while creating objects, we can assign unique values to each object while sharing the same structure defined by the same class.<\/p>\n\n\n\n<p><strong>Arrays: <\/strong><\/p>\n\n\n\n<p>An Array, on the other hand, is a dynamic data structure that provides flexibility to store multiple data values under a single variable. Data are stored in these variables by index or as key-value pairs (where the key represents the index position of the data value).<\/p>\n\n\n\n<p>The primary purpose of utilizing arrays is to perform various data manipulation tasks, such as sorting, filtering, searching, merging, mapping, and many more.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Also Read:<\/strong> <em><a href=\"https:\/\/www.guvi.in\/blog\/guide-to-find-php-array-length-with-examples\/\" target=\"_blank\" rel=\"noreferrer noopener\">PHP Array Length<\/a><\/em><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong><em>Learning individual concepts is one thing, applying them in real production systems is another. HCL GUVI&#8217;s <\/em><\/strong><em><a href=\"https:\/\/www.guvi.in\/zen-class\/full-stack-development-course\/?utm_source=blog&amp;utm_medium=hyperlink&amp;utm_campaign=convert-object-to-array-in-php\" target=\"_blank\" rel=\"noreferrer noopener\">Software and AI Engineer Programme<\/a><\/em><strong><em> takes you beyond the basics into full stack or backend engineering, covering React, Node.js, MongoDB, APIs, and AI-assisted development, backed by mentor support and real-world projects. Enroll now and build the skills top tech companies actually hire for!<\/em><\/strong><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Why Convert Object to Array?<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1200\" height=\"630\" src=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/Convert-an-object-1-1200x630.webp\" alt=\"\" class=\"wp-image-134416\" srcset=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/Convert-an-object-1-1200x630.webp 1200w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/Convert-an-object-1-300x157.webp 300w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/Convert-an-object-1-768x403.webp 768w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/Convert-an-object-1-1536x806.webp 1536w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/Convert-an-object-1-150x79.webp 150w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/Convert-an-object-1.webp 2048w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" title=\"\"><\/figure>\n\n\n\n<p>Converting an object into an array in <a href=\"https:\/\/www.guvi.in\/blog\/what-is-php\/\" target=\"_blank\" rel=\"noreferrer noopener\">PHP<\/a> is crucial because arrays are more flexible and easier to use for various programming tasks. It lets developers quickly access and modify data using PHP&#8217;s built-in functions. <\/p>\n\n\n\n<p>In addition, converting objects into arrays gives you more control and efficiency in processing data, which makes your PHP code cleaner, faster, and easier to maintain.&nbsp;<\/p>\n\n\n\n<p>This conversion of variables is necessary, especially in practical situations where data needs to be manipulated, analyzed, and presented to the end users based on their dynamic interactions (event triggering).<\/p>\n\n\n\n<p><strong><em>Real-World Example<\/em><\/strong><\/p>\n\n\n\n<p>In an <a href=\"https:\/\/www.guvi.in\/blog\/create-your-own-ecommerce-website-like-amazon\/\" target=\"_blank\" rel=\"noreferrer noopener\">e-commerce website<\/a>, you know how vital the product catalog section is. To display that section with essential details such as descriptions, user ratings and reviews, high-quality images, and brief video elements, APIs are integrated into the code to fetch information from the <a href=\"https:\/\/www.guvi.in\/blog\/how-do-servers-handle-requests\/\" target=\"_blank\" rel=\"noreferrer noopener\">server<\/a> database.&nbsp;<\/p>\n\n\n\n<p>When the data is fetched, the <a href=\"https:\/\/aws.amazon.com\/what-is\/api\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">API<\/a> returns the product data as an object. Now, for displaying those products in a structured fashion, features such as product sorting, category filters, and popular items recommendations need to be added.\u00a0<\/p>\n\n\n\n<p><span style=\"box-sizing: border-box; margin: 0px; padding: 0px;\">To enable this feature, converting the data objects into arrays is essential (through functions like&nbsp;<strong>array_filter(), usort(), array_map()<\/strong>, etc.), as it improves the application&#8217;s performance and speed<\/span>.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Also Read: <\/strong><em><a href=\"https:\/\/www.guvi.in\/blog\/php-vs-newer-back-end-languages\/\" target=\"_blank\" rel=\"noreferrer noopener\">Is PHP Dead in 2026?<\/a><\/em><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong><em>Ready to go beyond the basics? HCL GUVI&#8217;s <\/em><\/strong><em><a href=\"https:\/\/www.guvi.in\/courses\/web-development\/php-developer-bootcamp\/?utm_source=blog&amp;utm_medium=hyperlink&amp;utm_campaign=convert-object-to-array-in-php\" target=\"_blank\" rel=\"noreferrer noopener\">PHP Developer Bootcamp<\/a> <\/em><strong><em>covers everything from core PHP to OOP, databases, and security practices. Join today and turn what you&#8217;re learning into a real, job-ready skill.<\/em><\/strong><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Techniques to Convert an Object to an Array in PHP<\/strong><\/h2>\n\n\n\n<p>The following are the most effective conversion techniques:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. Type Casting <\/strong><strong>(array)$object<\/strong><\/h3>\n\n\n\n<p>It is the simplest and fastest method for converting an object to an array in PHP. In this method, you just have to place the array variable before the object, and the PHP language will interpret it automatically and transform it into an associative array.<\/p>\n\n\n\n<p>This is the ideal way to convert an object when it&#8217;s small and not deeply nested.<\/p>\n\n\n\n<p><strong><em>How to Execute:<\/em><\/strong><strong><em> <\/em><\/strong>First, take your object and place <strong>(array) <\/strong>before it \u2014 PHP will automatically convert all its properties into an associative array.<\/p>\n\n\n\n<p><strong><em>(Code)<\/em><\/strong><\/p>\n\n\n\n<p>{<\/p>\n\n\n\n<p><strong>Input:<\/strong><\/p>\n\n\n\n<p>&lt;?php<\/p>\n\n\n\n<p>$person = (object) [<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&#8216;name&#8217; =&gt; &#8216;John&#8217;,<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&#8216;age&#8217; =&gt; 25<\/p>\n\n\n\n<p>];<\/p>\n\n\n\n<p>$array = (array) $person;<\/p>\n\n\n\n<p>print_r($array);<\/p>\n\n\n\n<p>?&gt;<\/p>\n\n\n\n<p>______________________________________<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<p>Array ( [name] =&gt; John [age] =&gt; 25 )<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. Using <\/strong><strong>get_object_vars()<\/strong><\/h3>\n\n\n\n<p>It is a function that is utilized for returning all the public properties of an object in the form of an associative array.<\/p>\n\n\n\n<p>It is beneficial only when the data are stored in public variables rather than in private ones. Sensitive properties won&#8217;t be displayed in the output; in a way, they are protected.<\/p>\n\n\n\n<p><strong><em>How to Execute: <\/em><\/strong>You call the <strong>get_object_vars()<\/strong> function and pass your object inside it \u2014 this function then collects all the object\u2019s public properties and returns them as an array.<\/p>\n\n\n\n<p><strong><em>(Code)<\/em><\/strong><\/p>\n\n\n\n<p>{<\/p>\n\n\n\n<p><strong>Input:<\/strong><\/p>\n\n\n\n<p>&lt;?php<\/p>\n\n\n\n<p>$person = (object) [<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&#8216;name&#8217; =&gt; &#8216;John&#8217;,<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&#8216;age&#8217; =&gt; 25<\/p>\n\n\n\n<p>];<\/p>\n\n\n\n<p>$array = get_object_vars($person);<\/p>\n\n\n\n<p>print_r($array);<\/p>\n\n\n\n<p>?&gt;<\/p>\n\n\n\n<p>______________________________________<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<p>Array ( [name] =&gt; John [age] =&gt; 25 )<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. Using<\/strong><strong> json_encode() <\/strong><strong>and <\/strong><strong>json_decode()<\/strong><\/h3>\n\n\n\n<p>For nested and complex objects, this method is the most effective way to convert an object into a JSON string and then decode it into an array variable.<\/p>\n\n\n\n<p><span style=\"box-sizing: border-box; margin: 0px; padding: 0px;\">Because it supports deep conversion, it&#8217;s often used for&nbsp;<a href=\"https:\/\/www.guvi.in\/blog\/data-handling-with-big-data-and-dbms\/\" target=\"_blank\" rel=\"noopener\">handling data<\/a>&nbsp;<\/span>from <a href=\"https:\/\/www.guvi.in\/blog\/api-response-structure-best-practices\/\" target=\"_blank\" rel=\"noreferrer noopener\">APIs<\/a> or JSON responses.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n\n\n\n<p><strong><em>How to Execute:<\/em><\/strong><strong><em> <\/em><\/strong>First, convert the object into a JSON string using <strong>json_encode()<\/strong>, then immediately decode it back into an array with <strong>json_decode() <\/strong>by setting the second parameter to <strong>true<\/strong>.<\/p>\n\n\n\n<p><strong><em>(Code)<\/em><\/strong><\/p>\n\n\n\n<p>{<\/p>\n\n\n\n<p><strong>Input:<\/strong><\/p>\n\n\n\n<p>&lt;?php<\/p>\n\n\n\n<p>$person = (object) [<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&#8216;name&#8217; =&gt; &#8216;John&#8217;,<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&#8216;details&#8217; =&gt; (object) [<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8216;age&#8217; =&gt; 25,<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8216;city&#8217; =&gt; &#8216;New York&#8217;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;]<\/p>\n\n\n\n<p>];<\/p>\n\n\n\n<p>$array = json_decode(json_encode($person), true);<\/p>\n\n\n\n<p>print_r($array);<\/p>\n\n\n\n<p>?&gt;<\/p>\n\n\n\n<p>______________________________________<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<p>Array ( [name] =&gt; John [details] =&gt; Array ( [age] =&gt; 25 [city] =&gt; New York ) )<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. Using a<\/strong><strong> Loop (Manual Conversion)<\/strong><\/h3>\n\n\n\n<p>In this conversion technique, you have to manually go through each property inside the object using a <strong>foreach<\/strong> <strong>loop<\/strong> and store it in an empty array.&nbsp;<\/p>\n\n\n\n<p>This gives you complete control over the conversion. Here, you can easily skip, rename, or modify the keys while the conversion process is taking place.<\/p>\n\n\n\n<p><strong><em>How to Execute:<\/em><\/strong><strong><em> <\/em><\/strong>You loop through the object\u2019s properties using <strong>foreach <\/strong>and, for each <strong>key-value<\/strong> pair, store them in a new array \u2014 manually building your array step by step.<\/p>\n\n\n\n<p><strong><em>(Code)<\/em><\/strong><\/p>\n\n\n\n<p>{<\/p>\n\n\n\n<p><strong>Input:<\/strong><\/p>\n\n\n\n<p>&lt;?php<\/p>\n\n\n\n<p>$person = (object) [<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&#8216;name&#8217; =&gt; &#8216;John&#8217;,<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&#8216;age&#8217; =&gt; 25<\/p>\n\n\n\n<p>];<\/p>\n\n\n\n<p>$array = [];<\/p>\n\n\n\n<p>foreach ($person as $key =&gt; $value) {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;$array[$key] = $value;<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p>print_r($array);<\/p>\n\n\n\n<p>?&gt;<\/p>\n\n\n\n<p>______________________________________<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<p>Array ( [name] =&gt; John [age] =&gt; 25 )<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5. Using a <\/strong><strong>Custom Function (object_to_array())<\/strong><\/h3>\n\n\n\n<p>Designing a custom function tailored to your problem objective is the most flexible and reusable approach.<\/p>\n\n\n\n<p>Utilizing a custom function can help you convert both simple and nested object types into arrays. And you can implement it anywhere in your program without the tedious process of repeating or rewriting code.<\/p>\n\n\n\n<p><strong><em>How to Execute:<\/em><\/strong><strong><em> <\/em><\/strong>You create a reusable function that checks if the value is an object or array, and then <strong>recursively<\/strong> converts everything \u2014 including <strong>nested objects <\/strong>\u2014 into arrays automatically.<\/p>\n\n\n\n<p><strong><em>(Code)<\/em><\/strong><\/p>\n\n\n\n<p>{<\/p>\n\n\n\n<p><strong>Input:<\/strong><\/p>\n\n\n\n<p>&lt;?php<\/p>\n\n\n\n<p>function object_to_array($obj) {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;if (is_object($obj)) {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$obj = get_object_vars($obj);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;if (is_array($obj)) {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return array_map(&#8216;object_to_array&#8217;, $obj);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;return $obj;<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p>$person = (object) [<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&#8216;name&#8217; =&gt; &#8216;John&#8217;,<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&#8216;details&#8217; =&gt; (object) [<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8216;age&#8217; =&gt; 25,<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8216;city&#8217; =&gt; &#8216;New York&#8217;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;]<\/p>\n\n\n\n<p>];<\/p>\n\n\n\n<p>$array = object_to_array($person);<\/p>\n\n\n\n<p>print_r($array);<\/p>\n\n\n\n<p>?&gt;<\/p>\n\n\n\n<p>______________________________________<\/p>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<p>Array ( [name] =&gt; John [details] =&gt; Array ( [age] =&gt; 25 [city] =&gt; New York ) )<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>PHP Object to Array: Methods Compared<\/strong><\/h2>\n\n\n\n<p>Here are the most effective ways to convert object to array in PHP, along with when to use each one:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th><strong>Method<\/strong><\/th><th><strong>Syntax Example<\/strong><\/th><th><strong>What It Does<\/strong><\/th><th><strong>Ideal For<\/strong><\/th><th><strong>Performance<\/strong><\/th><\/tr><\/thead><tbody><tr><td><strong>Type Casting<\/strong><\/td><td><code>(array) $object<\/code><\/td><td>Converts the object into an array by simply placing (array) before it<\/td><td>Small, non-nested objects<\/td><td>Fastest<\/td><\/tr><tr><td><strong>get_object_vars()<\/strong><\/td><td><code>get_object_vars($object)<\/code><\/td><td>Returns all public properties of an object as an array<\/td><td>Objects with public properties only<\/td><td>Fast<\/td><\/tr><tr><td><strong>json_encode() + json_decode()<\/strong><\/td><td><code>json_decode(json_encode($object), true)<\/code><\/td><td>Converts the object to JSON, then decodes it back into an array<\/td><td>Nested\/complex objects, API data<\/td><td>Moderate<\/td><\/tr><tr><td><strong>Manual Loop (foreach)<\/strong><\/td><td><code>foreach ($object as $key =&gt; $value)<\/code><\/td><td>Loops through each property and manually builds a new array<\/td><td>Full control over key\/value handling<\/td><td>Moderate<\/td><\/tr><tr><td><strong>Custom Function<\/strong><\/td><td><code>object_to_array($object)<\/code><\/td><td>Recursively converts objects (including nested ones) into arrays<\/td><td>Reusable, recursive nested conversions<\/td><td>Moderate to Slow<\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\"><strong>Quick comparison of the 5 methods to convert object to array in PHP.<\/strong><\/figcaption><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Common Errors When Converting Object to Array in PHP<\/strong><\/h2>\n\n\n\n<p>A few mistakes come up often when developers convert object to array in PHP, especially with private properties or nested data. Here&#8217;s what to watch out for:<\/p>\n\n\n\n<ul>\n<li><strong>Mangled keys with type casting:<\/strong> Casting (array) $object doesn&#8217;t skip private or protected properties cleanly. It includes them, but with strange key prefixes that make them difficult to access normally.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>class Person {\n    private $name = \"John\";\n}\n$array = (array) new Person();\nprint_r($array); \n\/\/ Key shows up as \"\\0Person\\0name\" instead of \"name\"\n<\/code><\/pre>\n\n\n\n<ul>\n<li><strong>Private properties skipped silently:<\/strong> get_object_vars() only returns public properties when called from outside the class. Private and protected properties simply won&#8217;t show up, and PHP won&#8217;t warn you.<\/li>\n\n\n\n<li><strong>Missing data with json_encode():<\/strong> Since json_encode() only serializes public properties by default, private or protected values quietly disappear when you convert object to array this way. It&#8217;s easy to miss unless you check the output carefully.<\/li>\n\n\n\n<li><strong>Nested objects stay unconverted:<\/strong> A simple (array) cast only converts the top level. Any objects nested inside remain objects unless you use a recursive approach, like combining json_decode() with json_encode(), or writing a custom function.<\/li>\n\n\n\n<li><strong>Null results from invalid data:<\/strong> If the object holds data PHP can&#8217;t encode, such as circular references, json_encode() fails silently and json_decode() returns null instead of a proper array.<\/li>\n<\/ul>\n\n\n\n<p><strong>Note:<\/strong> For objects with private properties or deep nesting, a custom recursive function or the json_encode()\/json_decode() combination is a safer way to convert object to array without silently losing data.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>Ultimately, converting an object to an array in PHP is crucial for ensuring code adaptability across different situations. From handling API data and processing JSON to managing template rendering and configuring file parsing, each task becomes simpler after this conversion.&nbsp;<\/p>\n\n\n\n<p>We covered practical techniques for converting an object to an array, along with code snippet examples. I hope this blog helped guide you through all aspects of this conversion. To explore more interesting topics, visit HCL GUVI&#8217;s <a href=\"https:\/\/www.guvi.in\/blog\/\" target=\"_blank\" rel=\"noreferrer noopener\">comprehensive and insightful blog page<\/a> to satisfy your curiosity.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>FAQs<\/strong><\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1787211087674\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">1. How do you convert an object to array in PHP?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>The simplest way to convert object to array in PHP is type casting: <code>(array) $object<\/code>. For nested objects, use <code>json_decode(json_encode($object), true)<\/code> instead.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1787211094108\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">2. Why does casting an object to array show strange keys?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>When you convert object to array using a simple cast, private and protected properties get prefixed with hidden characters in their key names. Stick to public properties, or use <code>get_object_vars()<\/code> for cleaner output.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1787211095338\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">3. Can you convert a nested object to array in PHP?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Yes, but a basic type cast won&#8217;t work for nested data. Use <code>json_decode(json_encode($object), true)<\/code> or write a recursive custom function to convert object to array at every level.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1787211096183\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">4. What is the fastest way to convert PHP object to array?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Type casting <code>(array) $object<\/code> is the fastest method to convert PHP object to array, but it only works well for simple, non-nested objects.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1787211097858\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">5. Does json_encode() include private properties when converting?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>No. json_encode() only serializes public properties by default, so private and protected data gets left out when you convert object to array this way.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>To convert object to array in PHP, you can use built-in functions like type casting, get_object_vars(), or json_decode() depending on how simple or nested your data is. Since each method behaves differently, the right choice depends on your specific use case. That&#8217;s exactly what this guide covers, 5 practical techniques with code examples, along with [&hellip;]<\/p>\n","protected":false},"author":64,"featured_media":134426,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[294,907],"tags":[],"views":"2951","authorinfo":{"name":"Abhishek Pati","url":"https:\/\/www.guvi.in\/blog\/author\/abhishek-pati\/"},"thumbnailURL":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/10\/How-to-Convert-Object-to-Array-in-PHP-300x116.webp","_links":{"self":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/89038"}],"collection":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/users\/64"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/comments?post=89038"}],"version-history":[{"count":26,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/89038\/revisions"}],"predecessor-version":[{"id":139324,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/89038\/revisions\/139324"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media\/134426"}],"wp:attachment":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media?parent=89038"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/categories?post=89038"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/tags?post=89038"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}