All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

Stdclass Object To Array PHP

Last Updated : Mar 11, 2024

Stdclass Object To Array PHP

In this tutorial we will show you the solution of stdclass object to array PHP, to achieve stdclass object to an array conversions we need to collects stdclass all properties and use those properties to make associative array with values.

In php converting stdclass object to array we need to use inbuilt function of get_object_vars() and array_map() functions.

Step By Step Guide On Stdclass Object To Array PHP :-

Here we used get_object_vars() and array_map() functions for converting stdclass object to array using php.

The get_object_vars() function is an inbuilt function in php that is used to get the properties of the given object.

When an object is made, it has some properties. An associative array of properties of the mensioned object is returned by the function. If there is no property then it returns NULL.

The array_map() function sends each value of an array to a user-made function, and returns an array with new values, given by the user-made function.

$obj=new stdClass;
$fun = convert_object_array($obj);
function convert_object_array($data){
        return array_map(__FUNCTION__,$data);
        return $data;
  1. A php script can be placed anywhere in the document. A php script starts with <?php and end with ?>.
  2. The default file extension for php files is “.php” and php statements end with ‘;’ semicolon.
  3. First we need to create stdclass object it refers variable ‘$obj’ and ‘$’ symbol must add before variable name when we define a variable in php.
  4. Then we gives property for that object namely (name,age,city) and those property values are (‘Prawin’,25,’Veerapandi’). We calling function ‘convert_object_array()’ for conversion and result stored to variable ‘$fun’ then printed using ‘print_r()’ function.
  5. In convert_object_array() function we passed our stdclass object ‘$obj’ to this function here we take it as ‘$data’. Here we used to two if() condition for if our passed value is a object then we need to convert as array, another for converted array sends to user made function for make it as array with new values.
  6. First If() condition checks whether ‘$data’ is a object by is_object() method, if its object means we collects all properties of object and stored to variable ‘$data’.
  7. Next if() checks whether our ‘$data’ is an array if array means then using ‘array_map()’ function we makes new array with those properties for sends to user made function and result returns to variable ‘$fun’.
  8. Otherwise it’s not done any process and returns to function because its already an associative array with values.
  9. Here we used print_r for printing result on webpage browser and in php we have more inbuilt functions for display value or string on webpage.

Conclusion :-

In conclusion we are able to know how to covert stdclass object to array using php.

When work with php we need to create and process php files at server location and then we need to start the server before execute the program.

When we executing this program on browser it returns converted an associative array as a result.

For display value or string as a result on webpage we can use print, alertbox or echo methods also available in php.

I hope this tutorial on stdclass object to array PHP helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪