note 97555 added to book.simplexml
| From: | Ashok dot 893 at gmail dot com | Date: | Mon, 26 Apr 2010 05:48:13 +0000 |
| Subject: | note 97555 added to book.simplexml | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-168635@lists.php.net to get a copy of this message | ||
This is very simple way to convert all applicable objects into associative array. This works with
not only SimpleXML but any kind of object. The input can be either array or object. This function
also takes an options parameter as array of indices to be excluded in the return array. And keep in
mind, this returns only the array of non-static and accessible variables of the object since using
the function get_object_vars().
<?php
function objectsIntoArray($arrObjData, $arrSkipIndices = array())
{
$arrData = array();
// if input is object, convert into array
if (is_object($arrObjData)) {
$arrObjData = get_object_vars($arrObjData);
}
if (is_array($arrObjData)) {
foreach ($arrObjData as $index => $value) {
if (is_object($value) || is_array($value)) {
$value = objectsIntoArray($value, $arrSkipIndices); // recursive call
}
if (in_array($index, $arrSkipIndices)) {
continue;
}
$arrData[$index] = $value;
}
}
return $arrData;
}
?>
Usage:
<?php
$xmlUrl = "feed.xml"; // XML feed file/URL
$xmlStr = file_get_contents($xmlUrl);
$xmlObj = simplexml_load_string($xmlStr);
$arrXml = objectsIntoArray($xmlObj);
print_r($arrXml);
?>
----
Server IP: 69.147.83.197
Probable Submitter: 122.174.127.87
----
Manual Page -- http://www.php.net/manual/en/book.simplexml.php
Edit -- https://master.php.net/note/edit/97555
Del: integrated -- https://master.php.net/note/delete/97555/integrated
Del: useless -- https://master.php.net/note/delete/97555/useless
Del: bad code -- https://master.php.net/note/delete/97555/bad+code
Del: spam -- https://master.php.net/note/delete/97555/spam
Del: non-english -- https://master.php.net/note/delete/97555/non-english
Del: in docs -- https://master.php.net/note/delete/97555/in+docs
Del: other reasons-- https://master.php.net/note/delete/97555
Reject -- https://master.php.net/note/reject/97555
Search -- https://master.php.net/manage/user-notes.php