Convert Array To Object With PHP

Convert Array To Object With PHP

Today, We want to share with you Convert Array To Object With PHP.In this post we will show you PHP – Convert Array to Object with stdClass, hear for Cast or Convert an Array to Object Using 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.

Convert Array To Object using PHP

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

As I will cover this Post with live Working example to develop convert multidimensional array to object php, so the convert associative array to object php for this example is following below.

  Vuejs Clone Array Example with Demo

Example : Convert an array to an Object in PHP

Example 1: Using Type Casting

<?php
$all_data = (object)array('Product1','Product2','Product3','Product4');
print_r($all_data);
//output : stdClass Object ( [0] => Product1 [1] => Product2 [2] => Product3 [3] => Product4 )
?>

Example 2: Using array_map()

<?php
$array = array('Category1' => array('subcat1','subcat2'),'Category2','Category3','Category4');
 
$object = (object)array_map(function($item) { return is_array($item) ? (object)$item :  $item;  }, $array);
 
print_r($object);
 
// output : stdClass Object ( [Category1] => stdClass Object ( [0] => subcat1 [1] => subcat2 ) [0] => Category2 [1] => Category3 [2] => Category4 )

?>

Web Programming Tutorials Example with Demo

Read :

Summary

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

I hope you get an idea about Convert Array To Object.
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.

  PHP Convert Multidimensional Array To Object