473,769 Members | 3,352 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 13781
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
$nestedObjec t 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($neste dAA);

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

$nestedObject= new stdClass();
array_to_obj($n estedAA, $nestedObject);

print_r($nested Object);

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.andyhsoftwa re.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
1770
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 everytime the operator is used to make a change. I know you can derive from Array, but you cannot directly override the operator. Any way to make this possible? // possible definition of 'SpecialArray' ... function OnModify()
3
11725
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 contents of textboxes for each row are stored in an array. When i click the delete button it should delete the row from the table and also delete the row from the array that contains the data, but the problem is it is not deleted from the array...
1
19200
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
3692
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 elements of the same array object, or one past the last element of the array object, the evaluation shall not produce an overflow; otherwise, the
1
10018
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# language and the .NET framework:) I got some experience of ANSI C where you declare an array in stack, so: int myArray; would allocate a continous piece of memory in stack which can contain 10 integers in total.
6
4156
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
2827
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, where the correct type is given by a Type variable. So given is: string strValue; Type typeValue;
1
6813
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 bandingCalculator($properties){ $iterator = $properties->getIterator();
2
4500
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 convert the array using JSON, but I keep getting a 422(Unprocessable Entity) error. Im not sure what is wrong. function test() { for(i=1;i<22;i++){ if (document.getElementById(i+'d').className ==
18
43124
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 byte array (byte). Can anyone tell me how to convert byte array to byte pointer in CSharp? Please show me an simple example about how to do it. Thank you for your help
0
9579
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9416
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10199
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10032
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9979
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7393
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5293
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5433
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3551
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.