PHP Convert Multidimensional Array To Object

Today, We want to share with you PHP Convert Multidimensional Array To Object.In this post we will show you PHP – Convert mixed array & objects recursively, hear for Multidimensional Array to Object Conversion we will give you demo and example for implement.In this post, we will learn about Map and object: converting nested objects using PHP with an example.

PHP Convert Multidimensional Array To Object

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

As I will cover this Post with live Working example to develop PHP stdClass to Array and Array to stdClass, so the PHP – Convert Array to Object with stdClass for this example is following below.

First of all check my Best 3 Ways To PHP Array to Object Conversion step by step Below Link- PHP Convert An Array To An Object Example

Also Read :  PHP nested forloop - Exercises pyramid program Solution

Example: Non-Nested Array to Object

here simple and Best way to convert an array to object using PHP Examples


$arr= array(
    'laptop' => 'appleMarutiMaruti',
    'book' => 'MarutiappleMaruti',
    'tv' => 'MarutiMarutiapple',
);

$get_object = (object) $arr;

print_r($get_object);

/*
Output

stdClass Object
(
    [laptop] => appleMarutiMaruti
    [book] => MarutiappleMaruti
    [tv] => MarutiMarutiapple
)
*/

//Use it as
echo $get_object->book;

Example 1: PHP Multidimensional Array to Object

Best Way to Multidimensional Array to Object Convert using PHP JSON Encode/Decode

PHP Array: Associative, Multidimensional

$get_object = json_decode(json_encode($array));

First, the json_encode($array) converts the entire multi-dimensional array to a JSON string. Then the json_decode($string) will convert the JSON string to a stdClass object.

Example 2: Convert PHP Multidimensional Array to Object using array_map

2. Convert Multidimensional Array to Object using array_map

Accessing All Array Elements in Nested Arrays

function multi_dimensional_arr_to_obj($arr){
    return (is_array($arr) ? (object) array_map( __FUNCTION__, $arr) : $arr);
}

$get_object = multi_dimensional_arr_to_obj($array);

Example 3 : PHP Multi-dimensional Array to Object

PHP Custom Methods to Convert(PHP: Recursively convert an object to an array) PHP Multi-dimensional Array to Object Example

Also Read :  if else statement in VueJS templates

PHP stdClass to Array and Array to stdClass

function arr_to_obj_recursive_data($arr) {
    if (is_array($arr)){
        $generat_array = array();
        foreach($arr as $k => $v) {
            if (is_integer($k)) {
                $generat_array['index'][$k] = arr_to_obj_recursive_data($v);
            }
            else {
                $generat_array[$k] = arr_to_obj_recursive_data($v);
            }
        }

        return (object) $generat_array;
    }

    // else maintain the type of $arr
	//PHP: Recursively convert an object to an array
    return $arr; 
}

$arr= array(
    'opacity' => '0.5',
    'Mobile',
    'car',
    'colors' => array ('Mobile','Car','Laptop', 'Book', 'Tv'),
    'laptop' => array('r' => 'apple', 'g' => 'Maruti', 'b' => 'Maruti'),
    'book' => array('r' => 'Maruti', 'g' => 'apple', 'b' => 'Maruti'),
    'tv' => array('r' => 'Maruti', 'g' => 'Maruti', 'b' => 'apple'),
);

$get_object = arr_to_obj_recursive_data($arr);

print_r($get_object);

echo $get_object->laptop->g;            // Maruti
echo $get_object->opacity;           // 0.5
echo $get_object->index{1};          // car
echo $get_object->colors->index{0};  // Mobile

Final Output : PHP – Convert Array to Object with stdClass

/*
stdClass Object
(
    [opacity] => 0.5
    [index] => Array
        (
            [0] => Mobile
            [1] => car
        )

    [colors] => stdClass Object
        (
            [index] => Array
                (
                    [0] => Mobile
                    [1] => Car
                    [2] => Laptop
                    [3] => Book
                    [4] => Tv
                )

        )

    [laptop] => stdClass Object
        (
            [r] => apple
            [g] => Maruti
            [b] => Maruti
        )

    [book] => stdClass Object
        (
            [r] => Maruti
            [g] => apple
            [b] => Maruti
        )

    [tv] => stdClass Object
        (
            [r] => Maruti
            [g] => Maruti
            [b] => apple
        )

)
*/

Angular 6 CRUD Operations Application Tutorials

Read :

Also Read :  PHP Google No CAPTCHA reCAPTCHA jQuery Ajax

Summary

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

I hope you get an idea about PHP Convert Multidimensional Array To Object.
I would like to have feedback on my Pakainfo.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.

Gondaliya Jaydeep

Hi, I’m Jaydeep Gondaliya, PHP developer.Pakainfo.com is the most popular Programming & Web Development blog. Our mission is to provide the best online resources on programming and web development.Any visitors of this site are free to browse our tutorials, live demos, Examples and download scripts.If you have a project that you want to get started, launch quickly, contact me. Email : [email protected]