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.

Read Also:  Simple Jquery Search Filter 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 :

Read Also:  Loop Binding in Vue.js With V-for

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.


Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  Set Cookies Get Cookies Delete Cookies with PHP
  2. Read Also:  PHP Convert stdclass object recursive
  3. Read Also:  How to Open URL in New Tab using Magento
  4. Read Also:  How to Open URL in New Tab using Node JS
  5. Read Also:  WordPress Plugin Activation Dependency Check
  6. Read Also:  Laravel 6 Custom Authentication System
  7. Read Also:  PHP Convert An Array To An Object Example
  8. Read Also:  Drag And Drop Multiple File Uploading using DropzoneJS and PHP Example
  9. Read Also:  Laravel 7 jQuery Ajax Image Upload Tutorial
  10. Read Also:  Convert Array To Object With PHP
CLOSEX