FAO: robmiller (re: Array -> Object convert.)

Caporegime
Joined
18 Oct 2002
Posts
29,493
Location
Back in East London
ages ago I remember you were looking for a way to convert $array['value'] into pretty $array->value objects, today I bumped into this on phpfreaks:
Code:
function arr2obj($arg_array) {
	$tmp = new stdClass; // start off a new (empty) object
	foreach ($arg_array as $key => $value) {
		if (is_array($value)) { // if its multi-dimentional, keep going :)
			$tmp->$key = arr2obj($value);
		} else {
			if (is_numeric($key)) { // can't do it with numbers :(
				die("Cannot turn numeric arrays into objects!");
			}
			$tmp->$key = $value;
		}
	}
	return $tmp; // return the object!
}

Untested personally, but I can't see it not working :)
 
Sweet, as if you remembered that. I can't even remember what I wanted it for now :o

I do remember that I eventually stole the code from WordPress though, I think.

Cheers!
 
Back
Top Bottom