Convert Array To Object With PHP

php Programming Technology

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.

List of all Google Adsense, VueJS, AngularJS, PHP, Laravel Examples.

Snippet Contents

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.

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.


Related FAQ

Here are some more FAQ related to this Article:

  1.   PHP Convert Multidimensional Array To Object
  2.   jQuery parseInt Convert String to Integer Number
  3.   Custom Alert and Confirm Dialog Box using jQuery and CSS
  4.   Simple Laravel Form Validation Example
  5.   Laravel Installer Create Web Application install scripts
  6.   AngularJS Confirm Password Validation Example
  7.   PHP Convert An Array To An Object Example
  8.   LINQ Left Join Example using C# Example
  9.   Laravel Redirect Back with() Message
  10.   PHP GET HTML source code WebPages using DOM Parser

Leave a Reply

Your email address will not be published. Required fields are marked *