I wrote a quick and simple function which allows you to quickly convert arrays to objects. It allows you to change an array like this :
$a[‘b’];
To an object like this :
$a -> b;
Below is the function. Have fun!
Tue, May 6, 2008
I wrote a quick and simple function which allows you to quickly convert arrays to objects. It allows you to change an array like this :
To an object like this :
Below is the function. Have fun!
[...] Оригинал: на lost-in-code.com [...]
July 23rd, 2008 at 3:14 pm
function array_to_object($array = array()) {
return (object) $array;
}
July 23rd, 2008 at 3:42 pm
@patrick
Thank you for the type casting example. It is useful!
December 2nd, 2008 at 12:35 am
This will not work with multi dimensional arrays.
December 2nd, 2008 at 1:12 am
@Hari
You are right, it will not work with multi-dimensional arrays.
You could create a recursive type of function with a break to handle that though.
Below is an example of how the array is converted to an object.
http://lost-in-code.com/wp-content/projects/php-array-to-object/
The example I wrote above uses type casting.
Either way, it only converts the first array instance and not the inner arrays.
Best,
Antonie
December 2nd, 2008 at 3:24 pm
or just use:
$object = (object)$array;
December 3rd, 2008 at 3:03 am
@james
Yes, that was the type casting method which was mentioned earlier. Its a quick way of doing it. The function in the article simply illustrates the “core” of such a function and is a good reference for writing a recursive function/method to convert multi-dimensional arrays.
December 3rd, 2008 at 7:11 am
yes ok, thank you. very interesting.
what do the curly brackets actually do under the covers there? {$akey}
December 3rd, 2008 at 11:18 am
@james
The curly brackets create variable variables.
It basically creates a variable with the name of the variable inside the brackets.
Let me give you an example. You have a variable $a with the value “b” and a variable $b with the value “a”. You could output the contents of the variable $b like this :
${$a}You can read more about it here : http://www.php.net/language.variables.variable
All the best,
Antonie
December 3rd, 2008 at 11:31 am
ok, thanks. I’ll go and twist my head round a lamppost and look at it again and I’m sure it’ll make more sense!
ok, done, understood now and I’m sure some poor sod reading some of my old spaghetti code would have benefited from me knowing that!
all the best,
December 3rd, 2008 at 12:11 pm
@james
Yeah, you can often benefit from it by creating more dynamic code. If you ever need some PHP assistance, feel free to hit me up on Skype as “Contrid”. Two minds are better than one and I have a good feel for the language.
Best,
Antonie
February 24th, 2009 at 7:28 pm
I wrote a class for allmost the same purpose.
I only meant to manypulate the initial array “as” an object. Not convert it entirely.
My purpose was to control arrays like $_SESSION in an objectual manner, while not having to convert them into local objects and the back into arrays.
It is fully recursive.
And uses PHP magic functions for most of the job.
class Array_Object
{
private $init;
function __construct(&$init)
{
if(empty($init))
session_start();
if(!empty($init))
$this->init = &$init;
}
function __get($name)
{
if(empty($name))
return $this->init;
else
if(is_array($this->init[$name]))
if($this->$name)
return $this->$name;
else
return $this->$name = new session($this->init[$name]);
else
return $this->init[$name];
}
function __set($name,$value)
{
if(get_class($value) == “session” && !empty($name))
$this->$name = &$value;
else if(empty($name))
$this->init = &$value;
else
if(is_array($value))
{
$this->$name = new session($value);
$this->init[$name] = &$value;
}
else
$this->init[$name] = &$value;
}
function __unset($name)
{
unset($this->$name);
}
function __destruct()
{
unset($this);
}
function index($no)
{
return $this->init[$no];
}
}
August 15th, 2009 at 3:42 am
objects are always good to me :
$db->tableName->fieldName;
$db->tableName->recordCount();
…
i don’t use $rsarray['fieldName'] anymore
September 21st, 2009 at 6:54 am
Nevermind, crappy message posting system. Geez, can’t seem to share my way of doing this.
December 3rd, 2009 at 9:18 am
For multidimensionnal arrays :
function object_to_array($mixed) {
if(is_object($mixed)) $mixed = (array) $mixed;
if(is_array($mixed)) {
$new = array();
foreach($mixed as $key => $val) {
$key = preg_replace(“/^\(.*)\/”,”",$key);
$new[$key] = object_to_array($val);
}
}
else $new = $mixed;
return $new;
}
December 16th, 2009 at 12:02 am
I’ve built my own array to object function, which uses references for optimal speed and memory usage. And it also supports multi-dimensional arrays.
You can see the code here: http://gist.github.com/257606
I’m thinking it would be even faster than the code you have posted.
January 20th, 2010 at 6:09 pm
Multidimensional worked for me though:
$obj = (object)array(“mood” => (object)array(“happy” => “lol”, “angry” => “grr”),”color” => (object)array(“blue” => “cold”, “red” => “hot”));
print $obj->mood->angry;
Thanks for this post btw! Was really helpfull!
February 5th, 2010 at 2:25 pm
I’m using the following:
foreach ($all as $value)
{
echo $value.”;
}
February 19th, 2010 at 10:10 am
Nice snippet Antonie.
Here’s a recursive version i created:
function toObject($array) {
$obj = new stdClass();
foreach ($array as $key => $val) {
$obj->$key = is_array($val) ? toObject($val) : $val;
}
return $obj;
}
February 23rd, 2010 at 10:43 am
How do you deal with numeric arrays?
June 9th, 2010 at 8:50 pm
in terms of performance which is better, object or array?
July 31st, 2010 at 6:14 am
Good Programming..
August 2nd, 2010 at 2:56 am
good programming