473,397 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Convert an associate array to an object?

With PHP 4, is there a way to convert an associative array to an object?

For example, suppose I have the following arrays inside a nested associative
array ($nestedAA):

$AA1['field1'] = 'fieldValue1';

$AA2['field2'] = 'fieldValue2';

$nestedAA['AA1'] = $AA1;
$nestedAA['AA2'] = $AA2;

I would like to take $nestedAA and convert it to an object called
$nestedObject with properties I could access like this:

echo $nestedObject->AA1->field1; # This would print 'fieldValue1'
echo $nestedObject->AA2->field2; # This would print 'fieldValue2'

How can I do this? Note, I would like a conversion method that I could use
with any nested associativee array and create the object dynamically (at
runtime).

Thanks,
Robert


Jul 17 '05 #1
1 13749
On Thu, 9 Jun 2005 12:59:12 -0400, "Robert Oschler" <no************@nospam.com>
wrote:
With PHP 4, is there a way to convert an associative array to an object?

For example, suppose I have the following arrays inside a nested associative
array ($nestedAA):

$AA1['field1'] = 'fieldValue1';

$AA2['field2'] = 'fieldValue2';

$nestedAA['AA1'] = $AA1;
$nestedAA['AA2'] = $AA2;

I would like to take $nestedAA and convert it to an object called
$nestedObject with properties I could access like this:

echo $nestedObject->AA1->field1; # This would print 'fieldValue1'
echo $nestedObject->AA2->field2; # This would print 'fieldValue2'

How can I do this? Note, I would like a conversion method that I could use
with any nested associativee array and create the object dynamically (at
runtime).


Something like:

<pre>
<?php
$AA1['field1'] = 'fieldValue1';
$AA2['field2'] = 'fieldValue2';

$nestedAA['AA1'] = $AA1;
$nestedAA['AA2'] = $AA2;

var_dump($nestedAA);

function array_to_obj($array, &$obj)
{
foreach ($array as $key => $value)
{
if (is_array($value))
{
$obj->$key = new stdClass();
array_to_obj($value, $obj->$key);
}
else
{
$obj->$key = $value;
}
}
return $obj;
}

$nestedObject= new stdClass();
array_to_obj($nestedAA, $nestedObject);

print_r($nestedObject);

echo $nestedObject->AA1->field1; # This would print 'fieldValue1'
echo $nestedObject->AA2->field2; # This would print 'fieldValue2'

?>
</pre>

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ben Katz | last post by:
Is it possible to have an Array object (or an object derived from Array) that is 'aware' of other code modifying its contents? I'd like to have such an "onModify" function process an array...
3
by: ms_chika | last post by:
Hi again to all, I just want to know how to delete array or array object in javascript? I have this problem, i have an html table that contains textboxes and delete button for each row, the...
1
by: Elioth | last post by:
Hi... I need to know how to convert Char Array to Byte Array and vice-versa in VB 2K5 Thanks for all help. Elioth
5
by: junky_fellow | last post by:
Hi, I discussed about this earlier as well but I never got any satisfactory answer. So, I am initiating this again. Page 84, WG14/N869 "If both the pointer operand and the result point to...
1
by: mattmao | last post by:
Hello everyone, this is my first thread in this .NET forum. Since I am studying C#.NET in this semester, I reckon this would be just the right place for my asking questions regarding the C#...
6
by: | last post by:
Hi, How do I convert a byte to an object? I am trying to use IADs PutEx and the last parameter required a variant array. Thanks, Alex
6
by: =?ISO-8859-1?Q?Norbert_P=FCrringer?= | last post by:
Hello, Imagine, that I've got a string variable containing a value of each possible value type (string, int32, double, ...). Now I want to convert the string to an object of the right type,...
1
by: BiraRai | last post by:
is it possible to add new variable to a array object at run time? the following code does not work. $iterator->current()-> = 1234; how can this be done? function...
2
by: David | last post by:
Hey can anyone help me convert a javascript array into a ruby array. Ive been struggling with this since friday to no avail. This is the function with the ajax.request call that is supposed to...
18
by: MrVS | last post by:
Hi, I have a C++ CLR class method that takes System::Byte *b as parameter argument. I want the CSharp caller pass a byte * to this function. But in the CSharp prorgram, I only managed to create a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.