Simple PHP Convert Object To An Array Examples

Today, We want to share with you Simple PHP Convert Object To An Array Examples.In this post we will show you wordpress plugin require another plugin, hear for convert array of objects to array php we will give you demo and example for implement.In this post, we will learn about PHP : Convert or Cast Array to Object & Object to Array with an example.

Simple PHP Convert Object To An Array Examples

There are the Following The simple About Simple PHP Convert Object To An Array Examples Full Information With Example and source code.

As I will cover this Post with live Working example to develop PHP – Convert Array to Object with stdClass, so the php convert multidimensional object to array is used for this example is following below.

Also Read :  POST JSON Data via PHP curl Example

Example 1: Convert an object to associative array in PHP

<?php
    /*** simple PHP create an object ***/
    $obj = new stdClass;
    $obj->movie = 'movie';
    $obj->song = 'song';
    $obj->tamilrokers = 'tamilrokers';

    /*** PHP cast the object ***/
    $array = (array) $obj;

    /*** Display the results ***/
    print_r( $array );
?>

Output

Array
(
    [movie] => movie
    [song] => song
    [tamilrokers] => tamilrokers
)

Example 2: Convert a PHP object to an associative array

<?php
    /*** a slightly more complex array ***/
    $obj = new stdClass;
    $obj->movie = 'movie';
    $obj->song = new stdClass;
    $obj->song->tamilrokers = 'tamilrokers';

    /*** PHP cast the object to array ***/
    $array = (array) $obj;

    /*** Display the results ***/
    print_r( $array );
?>

Output

Array
(
    [movie] => movie
    [song] => stdClass Object
	(
	    [tamilrokers] => tamilrokers
	)
)

Example 3 : Convert object to an array in PHP

<?php
    /***PHP a complex object ***/
    $obj = new stdClass;
    $obj->movie = new stdClass;
    $obj->movie->tamilrokers = 'tamilrokers';
    $obj->song = 'song';

    /**
    *
    * Convert an object to an array
    *
    * @param    object  $object The object to convert in PHP
    * @reeturn      array
    *
    */
    function objectToArray( $object )
    {
        if( !is_object( $object ) && !is_array( $object ) )
        {
            return $object;
        }
        if( is_object( $object ) )
        {
            $object = get_object_vars( $object );
        }
        return array_map( 'objectToArray', $object );
    }

    /*** PHP convert the array to object ***/
    $array = objectToArray( $obj );

    /*** Display the array ***/
    print_r( $array );
?>

Output

Array
(
    [movie] => Array
        (
            [tamilrokers] => tamilrokers
        )

    [song] => song
)

Web Programming Tutorials Example with Demo

Read :

Also Read :  jquery Bootstrap Creating Tooltips Example

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Simple PHP Convert Object To An Array Examples.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Reply

avatar
  Subscribe  
Notify of