2

我正在尝试从工作板 XML 提要中解析数据。基本的提要语法如下所示:

<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;
?>

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

  • 分析
    • 高级数据分析师
    • 其他类型的分析师
  • 信息技术
    • 基础设施管理员

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

4

2 回答 2

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;
?>
于 2012-08-22T17:03:56.793 回答
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);
于 2012-08-22T17:06:04.297 回答