使用PHP解析XML Feed:按类别分组输出

时间:2012-08-22 16:54:09

标签: php xml

我正在尝试解析作业板XML Feed中的数据。基本的feed语法如下所示:

<jobs>
<job>
    <title>
    <![CDATA[ Senior Data Analyst ]]>
    </title>
    <category>
    <![CDATA[ Analytics ]]>
    </category>
    <url>
    <![CDATA[
    http://www.mysite.com/careers/Senior-Data-Analyst.html
    ]]>
    </url>
</job>
<job>
    <title>
    <![CDATA[ Infrastructure Administrator ]]>
    </title>
    <category>
    <![CDATA[ Information Technology ]]>
    </category>
    <url>
    <![CDATA[
    http://www.mysite.com/careers/Infrastructure-Administrator.html
    ]]>
    </url>
</job>
</jobs>

我已经知道如何使用foreach循环解析基本信息:

<?php
    $jobs = simplexml_load_file('jobfeed.xml');

    foreach ($jobs as $jobDetails):
        $category = $jobDetails->category;
        $title = $jobDetails->title;
        $url = $jobDetails->url;

        echo '<li>Category: ',$category,'<br>Title: ',$title,'<br>URL: ',$url,'</li>';
    endforeach;
?>

但是我想按类别对每个作业进行分组,所以看起来像这样:

  • 分析
    • 高级数据分析师
    • 其他一些分析师
  • 信息技术
    • 基础架构管理员

但我不知道如何遍历类别数据并将输出分组。建议?感谢。

3 个答案:

答案 0 :(得分:1)

<?php
    $jobs = simplexml_load_file('jobfeed.xml');

    // We will store each category in an array. Each job in each category will be
    // an array storing the Title and URL.
    $categoryArray = array();

    // Loop over XML structure as before.
    foreach ($jobs as $jobDetails):
        $category = $jobDetails->category;
        $title = $jobDetails->title;
        $url = $jobDetails->url;

        // If the current category is not in the array yet, add it.
        if(empty($categoryArray[$category])) {
            $categoryArray[$category] = array();
        }
        // If the current category exists, add the job details to it.
        $categoryArray[$category][] = array(
            "Title" => $title, 
            "Url" => $url
        );
    endforeach;

    // Now we can loop over the sorted array, and output the data as required.
    foreach($categoryArray as $categoryName => $jobArray):
        echo '<li>Category: ' . $category;

        // Within each category, output an inner list for each job.
        echo '<ul>';    
        foreach($jobArray as $job) {
            echo '<li>Title: ' . $job["Title"] . '<br>URL: ' . $job["Url"] . '</li>';
        }

        echo '</ul>';
        echo '</li>';
    endforeach;
?>

答案 1 :(得分:1)

这不是一个新的答案......我想对这两个答案发表评论,以防有人像我一样偶然发现这个老问题,但没有足够的声誉来评论。

为了解决“isset或empty”中的“非法偏移类型”问题,我得到了两个答案,我稍微改变了xml节点的格式:

变化

$category = $jobDetails->category;

$category = "{$jobDetails->category}"

此外,在第二个答案中,类别名称将覆盖自身并继续使用最后的结果。变化:

foreach($categoryArray as $categoryName => $jobArray):
        echo '<li>Category: ' . $category;

为:

foreach($categoryArray as $categoryName => $jobArray):
        echo '<li>Category: ' . $categoryName;

答案 2 :(得分:0)

不是100%确定您要做的是什么,但您可以轻松地对输出进行分组:

function output_list($jobs = array())
{
    $data = array();

    foreach($jobs as $job_details)
    {
        $category = $job_details->category;
        $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);