dongliang9682 2014-02-23 19:39
浏览 23

我如何以这种格式获得PHP数组?

I am scraping HTML with DOM to create a custom RSS feed from an external website. I have all the values I need in an array called $jobs. I can print those values like this:

function jobscrape($title, $link, $root, $description, $job_location) {

$jobs = array();

$html = file_get_contents($link);
$doc = new DOMDocument();
libxml_use_internal_errors(TRUE);

if(!empty($html)) {

    $doc->loadHTML($html);
    libxml_clear_errors(); // remove errors for yucky html
    $xpath = new DOMXPath($doc);

    $row = $xpath->query($job_location);

    if ($row->length > 0) {

        foreach ($row as $job) {

            $jobs['title'] = $job->nodeValue;
            $jobs['description'] = "This is a description";
            $jobs['link'] = $job->getAttribute('href');

        }
    }
    else { echo "row is less than 0";}
}

else { echo "this is empty";}
}
}

However, I need the array in this format, where each 'sub-array' is one iteration of the three variables (I'm just using three here as an example):

$entries = array(
    array(
        "title" => "My first test entry",
        "description" => "This is the first article's description",
        "link" => "http://leolabs.org/my-first-article-url"
    ),
    array(
        "title" => "My second test entry",
        "description" => "This is the second article's description",
        "link" => "http://leolabs.org/my-second-article-url"
    ),
    array(
        "title" => "My third test entry",
        "description" => "This is the third article's description",
        "link" => "http://leolabs.org/my-third-article-url"
    )
);

UPDATE

After trying Durgesh's solution, this is my new code:

function jobscrape($title, $link, $root, $description, $job_location) {

header("Content-Type: application/rss+xml; charset=UTF-8");

$xml = new SimpleXMLElement('<rss/>');
$xml->addAttribute("version", "2.0");
$channel = $xml->addChild("channel");

$channel->addChild("title", $title);
$channel->addChild("link", $link);
$channel->addChild("description", "This is a description");
$channel->addChild("language", "en-us");

$html = file_get_contents($link);
$doc = new DOMDocument();
libxml_use_internal_errors(TRUE);

if(!empty($html)) {

    $doc->loadHTML($html);
    libxml_clear_errors(); // remove errors for yucky html
    $xpath = new DOMXPath($doc);

    $row = $xpath->query($job_location);

    if ($row->length > 0) {

        foreach ($row as $job) {

            $jobs = array();
            $entries = array();

            $jobs['title'] = $job->nodeValue;
            $jobs['description'] = "This is a description";
            $jobs['link'] = $job->getAttribute('href');

            array_push($entries,$jobs);

            foreach ($entries as $entry) {

                $item = $channel->addChild("item");
                $item->addChild("title", $entry['title']);
                $item->addChild("link", $entry['link']);
                $item->addChild("description", $entry['description']);

            }

            echo $xml->asXML();

        }
    }
    else { echo "row is less than 0";}
}

else {
    echo "this is empty";
}

}

However, my RSS is formatting incorrectly, adding the following to every <item> rather than just in the header:

<?xml version="1.0"?>
<rss version="2.0"><channel><title>Media Muppet</title><link>http://www.mediargh.com/jobs</link><description>This is a description</description><language>en-us</language>
  • 写回答

1条回答 默认 最新

  • dongpixi2648 2014-02-23 20:05
    关注

    If your $jobs giving proper array, You can just make $entries array by

    array_push($entries,$jobs);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记