Created
June 8, 2012 16:51
-
-
Save cam-gists/2896830 to your computer and use it in GitHub Desktop.
PHP: XML -> Associative Array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Parse XML feed => Assocative Array | |
*/ | |
//*----------------------------------DEBUG ----------------------------------------*/ | |
error_reporting(E_ALL); | |
ini_set("display_errors", 1); | |
//*----------------------------------DEBUG ----------------------------------------*/ | |
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; | |
} | |
$xmlUrl = "http://showcase.xmlteam.com/index.php/documents/fetch/fixture/event-stats/l/l.mlb.com/publisher/sportsnetwork.com/stylesheet"; // XML feed file/URL | |
$xmlStr = file_get_contents($xmlUrl); | |
$xmlObj = simplexml_load_string($xmlStr); | |
$arrXml = objectsIntoArray($xmlObj); | |
echo '<pre>'; | |
echo print_r($arrXml['sports-content']); | |
//print_r($arrXml); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment