Created
August 22, 2012 17:59
-
-
Save kmgdevelopment/3427969 to your computer and use it in GitHub Desktop.
Parse XML by Category
This file contains 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 | |
$jobs = simplexml_load_file('jobfeed.xml'); | |
function output_list($jobs = array()) | |
{ | |
$data = array(); | |
foreach($jobs->xpath('/jobs/job') as $job_details) | |
{ | |
$category = $job_details->department; | |
$title = $job_details->title; | |
$url = $job_details->url; | |
if(!isset($data[$category])) | |
{ | |
$data[$category] = array(); | |
} | |
$data[$category][] = '<li>Category: '.$category.'<br>Title: '.$title.'<br>URL: '.$url.'</li>'; | |
} | |
foreach($data as $category => $items) | |
{ | |
$data[$category] = '<li><ul>'.implode('', $items).'</ul></li>'; | |
} | |
return '<ul>'.implode('', $data).'</ul>'; | |
} | |
echo output_list($jobs); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
var_dump($data);
gives me this:array(0) { }
var_dump($category);
gives me this:object(SimpleXMLElement)#47 (0) { }
Both would imply that there is no data being returned for the
$category
variable, which confuses the hell out of me because if I do this:The categories are output perfectly fine.