Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何获得这种格式的PHP数组?_Php_Arrays - Fatal编程技术网

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

如何获得这种格式的PHP数组?,php,arrays,Php,Arrays,我正在用DOM抓取HTML,以便从外部网站创建自定义RSS提要。我在一个名为$jobs的数组中拥有所需的所有值。我可以这样打印这些值: function jobscrape($title, $link, $root, $description, $job_location) { $jobs = array(); $html = file_get_contents($link); $doc = new DOMDocument(); libxml_use_internal_errors(TRUE

我正在用DOM抓取HTML,以便从外部网站创建自定义RSS提要。我在一个名为
$jobs
的数组中拥有所需的所有值。我可以这样打印这些值:

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";}
}
}
但是,我需要这种格式的数组,其中每个“子数组”是三个变量的一个迭代(我这里仅使用三个作为示例):

更新

在尝试Durgesh的解决方案后,这是我的新代码:

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";
}

}
函数jobsrap($title、$link、$root、$description、$job\u location){
标题(“内容类型:应用程序/rss+xml;字符集=UTF-8”);
$xml=新的SimpleXMLElement(“”);
$xml->addAttribute(“版本”,“2.0”);
$channel=$xml->addChild(“channel”);
$channel->addChild(“title”,$title);
$channel->addChild(“link”,即$link);
$channel->addChild(“说明”,“这是说明”);
$channel->addChild(“语言”、“en-us”);
$html=文件获取内容($link);
$doc=新的DOMDocument();
libxml\u使用\u内部错误(TRUE);
如果(!empty($html)){
$doc->loadHTML($html);
libxml_clear_errors();//删除讨厌的html的错误
$xpath=新的DOMXPath($doc);
$row=$xpath->query($job\u位置);
如果($row->length>0){
foreach($row作为$job){
$jobs=array();
$entries=array();
$jobs['title']=$job->nodeValue;
$jobs['description']=“这是一个描述”;
$jobs['link']=$job->getAttribute('href');
数组推送($entries,$jobs);
foreach($entries作为$entry){
$item=$channel->addChild(“item”);
$item->addChild(“title”,$entry['title']);
$item->addChild(“链接”,$entry['link']);
$item->addChild(“description”,$entry['description']);
}
echo$xml->asXML();
}
}
否则{echo“行小于0”;}
}
否则{
回声“这是空的”;
}
}
但是,my的格式不正确,将以下内容添加到每个
中,而不仅仅是在标题中:

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

媒体Muppethttp://www.mediargh.com/jobsThis 这是我们的一个描述

如果您的
$jobs
提供了正确的数组,您可以通过

array_push($entries,$jobs);

$entries[]=$jobs
…感谢您的回复。这让我走到了一半——它又回到了一排。我该怎么循环呢?嘿,谢谢你的回答。这给了我与Marc的解决方案相同的结果-只返回一个结果。我插入到函数中的变量意味着在上面的
foreach
循环中将有多行输出。如何获得以我所追求的格式返回的相同行数?在哪里编写
array\u push
语句?它应该在forearch循环中。我已经用我的代码和它现在输出的内容进行了更新。那么现在有什么问题呢?
$entries
提供的数组是否与示例中所述的相同?
array_push($entries,$jobs);