Truemag

  • Categories
    • Tips And Tricks
    • Internet
    • PHP
    • Javascript
    • CSharp
    • SQL Server
    • Linux
  • Lastest Videos
  • Our Demos
  • About
  • Contact
  • Home
  • Write With Us
  • Job Request
Home PHP PHP Convert stdClass Object To Array And Array To stdClass Object

PHP Convert stdClass Object To Array And Array To stdClass Object

In some cases, especially while working with API or web service response. It often returns an object rather a string; therefore, you can not parse the result without converting.

In this tutorial, I’ll give you a simple function and example on how to:

  • 1. Convert stdClass Object to Array
  • 2. Convert Array to stdClass Object

1. PHP Function Convert stdClass Object to Array

<?php
 	function object2Array($d)
	{
		if (is_object($d))
		{
			$d = get_object_vars($d);
		}
 
		if (is_array($d))
		{
			return array_map(__FUNCTION__, $d);
		}
		else
		{
			return $d;
		}
	}
?>

<?php function object2Array($d) { if (is_object($d)) { $d = get_object_vars($d); } if (is_array($d)) { return array_map(__FUNCTION__, $d); } else { return $d; } } ?>

2. PHP Function Convert Array to stdClass Object

<?php
	function array2Object($d)
	{
		if (is_array($d))
		{
			return (object) array_map(__FUNCTION__, $d);
		}
		else
		{
			return $d;
		}
	}
?>

<?php function array2Object($d) { if (is_array($d)) { return (object) array_map(__FUNCTION__, $d); } else { return $d; } } ?>

3. Usage the object2Array and array2Object function

<?php
	$std = new stdClass;
 
	$std->a = "This is value for a";
	$std->b = "This is value for b";
	$std->c->c_std = new stdClass;
	$std->c->c_std->c1 = "This is value for c1 in c_std stdClass";
	$std->c->c_std->c2 = "This is value for c2 in c_std stdClass";
	$std->d = "This is value for d";
 
	print_r($std);
	echo "\r\n";
 
	$array = object2Array($std);
	print_r($array);
	echo "\r\n";
 
	$object = array2Object($array);
	print_r($object);
	echo "\r\n";
?>

<?php $std = new stdClass; $std->a = "This is value for a"; $std->b = "This is value for b"; $std->c->c_std = new stdClass; $std->c->c_std->c1 = "This is value for c1 in c_std stdClass"; $std->c->c_std->c2 = "This is value for c2 in c_std stdClass"; $std->d = "This is value for d"; print_r($std); echo "\r\n"; $array = object2Array($std); print_r($array); echo "\r\n"; $object = array2Object($array); print_r($object); echo "\r\n"; ?>

The output:

stdClass Object
(
    [a] => This is value for a
    [b] => This is value for b
    [c][/c] => stdClass Object
        (
            [c_std] => stdClass Object
                (
                    [c1] => This is value for c1 in c_std stdClass
                    [c2] => This is value for c2 in c_std stdClass
                )
 
        )
 
    [d] => This is value for d
)
 
Array
(
    [a] => This is value for a
    [b] => This is value for b
    [c][/c] => Array
        (
            [c_std] => Array
                (
                    [c1] => This is value for c1 in c_std stdClass
                    [c2] => This is value for c2 in c_std stdClass
                )
 
        )
 
    [d] => This is value for d
)
 
stdClass Object
(
    [a] => This is value for a
    [b] => This is value for b
    [c][/c] => stdClass Object
        (
            [c_std] => stdClass Object
                (
                    [c1] => This is value for c1 in c_std stdClass
                    [c2] => This is value for c2 in c_std stdClass
                )
 
        )
 
    [d] => This is value for d
)

stdClass Object ( [a] => This is value for a [b] => This is value for b [c][/c] => stdClass Object ( [c_std] => stdClass Object ( [c1] => This is value for c1 in c_std stdClass [c2] => This is value for c2 in c_std stdClass ) ) [d] => This is value for d ) Array ( [a] => This is value for a [b] => This is value for b [c][/c] => Array ( [c_std] => Array ( [c1] => This is value for c1 in c_std stdClass [c2] => This is value for c2 in c_std stdClass ) ) [d] => This is value for d ) stdClass Object ( [a] => This is value for a [b] => This is value for b [c][/c] => stdClass Object ( [c_std] => stdClass Object ( [c1] => This is value for c1 in c_std stdClass [c2] => This is value for c2 in c_std stdClass ) ) [d] => This is value for d )

Download the source code & example above.

Jul 26, 2011Hoan Huynh
Add Hyperlinks To Yahoo! AnswersPHP Call Web Service WSDL Example
You Might Also Like:
  • PHP Call Web Service WSDL Example
  • C# Check File Extension In Array Of Valid Extension
  • Validate Date With JQuery And Date Object
  • Convert NSString to Char
  • innerText May Not Work On Mozilla Firefox
  • Javascript remove vietnamese accents
  • String To Lower Case In PHP, JavaScript And .Net (CSharp)
  • String To Upper Case In PHP, JavaScript And .Net (CSharp)
  • Auto Convert Text To URL Link And MailTo Email Address With ASP.NET C#
  • How To Track Website With Multiple Google Analytisc Accounts
Hoan Huynh

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at [email protected]

9 years ago PHPAPI, array_map, get_object_vars, stdClass, web service1,008
0
GooglePlus
0
Facebook
0
Twitter
0
Digg
0
Delicious
0
Stumbleupon
0
Linkedin
0
Pinterest
Most Viewed
PHP Download Image Or File From URL
21,956 views
Notepad Plus Plus Compare Plugin
How To Install Compare Text Plugin In Notepad Plus Plus
19,805 views
Microsoft SQL Server 2008 Attach Remove Log
Delete, Shrink, Eliminate Transaction Log .LDF File
15,629 views
JQuery Allow only numeric characters or only alphabet characters in textbox
13,136 views
C# Read Json From URL And Parse/Deserialize Json
9,595 views
4 Rapid Development is a central page that is targeted at newbie and professional programmers, database administrators, system admin, web masters and bloggers.
Recent Posts
  • Essay Writers
  • College Essay Writers – The Way to Earn Your Essay in Demand
  • Buy Research Papers – How to See Them at a Bargain Price
  • How to Write a Research Paper For Sale
  • Essay Writing – Some Useful Suggestions for Writing Urgent Essays
Categories
  • CSharp (45)
  • Facebook Graph API (19)
  • Google API (7)
  • Internet (87)
  • iPhone XCode (8)
  • Javascript (35)
  • Linux (28)
  • MySQL (16)
  • PHP (84)
  • Problem Issue Error (29)
  • Resources (32)
  • SQL Server (25)
  • Timeline (5)
  • Tips And Tricks (141)
  • Uncategorized (69)
Recommended
  • Custom Software Development Company
  • Online Useful Tools
  • Premium Themes
  • VPS
2014 © 4 Rapid Development