note 91494 added to book.simplexml
| From: | walter at osu1 dot php dot net | Date: | Fri, 12 Jun 2009 14:58:21 +0000 |
| Subject: | note 91494 added to book.simplexml | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-156808@lists.php.net to get a copy of this message | ||
Working fix for the infamous SimpleXML + memcache bug.
Improves code at http://tinyurl.com/bmoon-simplexml
to actually work for arbitrary-depth structures. (Because neither the function nor the
json_decode(json_encode($obj)) hack listed there worked for me.)
There's probably some superfluous code in here that could be improved on -- but it works!
<?
# convert a structure that may include objects to a pure
# array-based structure (that can be stored in memcache)
# ... includes support for simplexml!
# (nb: may have problems with infinite recursive structs)
function enforce_array($obj) {
$array = (array)$obj;
if(empty($array)) {
$array = '';
}
else {
foreach($array as $key=>$value) {
if(!is_scalar($value)) {
if(is_a($value,'SimpleXMLElement')) {
$tmp = memcache_objects_to_array($value);
if(!is_array($tmp)) {
$tmp = ''.$value;
}
$array[$key] = $tmp;
}
else {
$array[$key] = enforce_array($value);
}
}
else {
$array[$key] = $value;
}
}
}
return $array;
}
?>
----
Server IP: 64.71.164.2
Probable Submitter: 87.194.70.98
----
Manual Page -- http://www.php.net/manual/en/book.simplexml.php
Edit -- https://master.php.net/note/edit/91494
Del: integrated -- https://master.php.net/note/delete/91494/integrated
Del: useless -- https://master.php.net/note/delete/91494/useless
Del: bad code -- https://master.php.net/note/delete/91494/bad+code
Del: spam -- https://master.php.net/note/delete/91494/spam
Del: non-english -- https://master.php.net/note/delete/91494/non-english
Del: in docs -- https://master.php.net/note/delete/91494/in+docs
Del: other reasons-- https://master.php.net/note/delete/91494
Reject -- https://master.php.net/note/reject/91494
Search -- https://master.php.net/manage/user-notes.php