Skip to content

Instantly share code, notes, and snippets.

@cam-gists
Created June 8, 2012 16:51
Show Gist options
  • Save cam-gists/2896830 to your computer and use it in GitHub Desktop.
Save cam-gists/2896830 to your computer and use it in GitHub Desktop.
PHP: XML -> Associative Array
<?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