Php 按日期对多个XML文件排序

Php 按日期对多个XML文件排序,php,Php,我有一个新闻小部件,它使用domxml拉入多个Xml文件来显示。我正试图找出如何让文章按日期顺序显示。以下是小部件的代码: $dh = opendir('./xml/'); $fileCount = 0; while ($file = readdir($dh) and $fileCount < 3) { if (preg_match("/^..?$/", $file)) { continue; } $open = "./xml/".$

我有一个新闻小部件,它使用domxml拉入多个Xml文件来显示。我正试图找出如何让文章按日期顺序显示。以下是小部件的代码:

$dh = opendir('./xml/');
    $fileCount = 0;
    while ($file = readdir($dh) and $fileCount < 3) {
    if (preg_match("/^..?$/", $file)) {
        continue;
    }
    $open = "./xml/".$file;
    $xml = domxml_open_file($open);
    //we need to pull out all the things from this file that we will need to 
    //build our links
    $root = $xml->root();
    $stat_array = $root->get_elements_by_tagname("status");
    $status = extractText($stat_array);
    $date_array = $root->get_elements_by_tagname("date");
    $date = extractText($date_array);
    $ab_array = $root->get_elements_by_tagname("abstract");
    $abstract = extractText($ab_array);
    $h_array = $root->get_elements_by_tagname("headline");
    $headline = extractText($h_array);
    $img_array = $root->get_elements_by_tagname("image");
    $image = extractText($img_array);
    $lead_array = $root->get_elements_by_tagname("para-intro");

    $para["intro"] = extractText($lead_array);

    if ($status != "live") {
        continue;
    }

    echo "<div class=\"col-md-12 newsbox\"><img style=\"margin-bottom: 10px;\" width=\"100%\" src=\"images/news/".$image."\"><div class=\"newsboxtext\"><a href=\"showArticle.php?file=".$file. "\"><h2 class=\"mainheadline2\"> ".$headline . "</h2></a><a href=\"showArticle.php?file=".$file . "\"><button  style=\"margin-top:5px;\" type=\"button\" class=\"btn btn-sm btn-default\">+ Read More</button></a></div><hr class=\"linedivider\">
</div>";

    $fileCount++;
}

但是我收到了这个错误。警告:usort要求参数1为数组,在

中给出null。请考虑将所有提取的标记值传递到多维数组中,$xmlData,然后对数组进行排序,然后从排序的数组中回显web html数据:

$r = 0;
$xml = new DOMDocument('1.0', 'UTF-8');

function extractText($tag) {
   foreach ($tag as $item) {
      $value = $item->nodeValue;
   }
   return $value;
}

while ($file = readdir($dh) and $fileCount < 3) {

    if (preg_match("/^..?$/", $file)) {
        continue;
    }

    $open = "./xml/".$file;
    $xml->load($open);

    //we need to pull out all the things from this file that we will need to 
    //build our links
    $xmldata = [];
    $xmldata[$r]['file'] = $file;

    $stat_array = $xml->getElementsByTagName("status");
    $xmldata[$r]['status'] = extractText($stat_array);

    $date_array = $xml->getElementsByTagName("date");
    $xmldata[$r]['date']= extractText($date_array);

    $ab_array = $xml->getElementsByTagName("abstract");
    $xmldata[$r]['abstract'] = extractText($ab_array);

    $h_array = $xml->getElementsByTagName("headline");
    $xmldata[$r]['headline'] = extractText($h_array);

    $img_array = $xml->getElementsByTagName("image");
    $xmldata[$r]['image'] = extractText($img_array);

    $lead_array = $xml->getElementsByTagName("para-intro");
    $xmldata[$r]['paraintro'] = extractText($lead_array);

    if ($status != "live") {
        continue;
    }

    $fileCount++;
    $r++;
}

// YYYY-MM-DD STRINGS STILL SORTS IN DAILY ORDER (I.E., '2015-01-01' < '2015-12-31')
usort($xmldata, function($a, $b) {
    return strcmp($a['date'], $b['date']);
});

$xmlcount = sizeof($xmldata[0]);

// ITERATE THROUGH LOOP EVERY NEEDED ITEM IN SORT ORDER OF ARRAY
for ($i = 0; $i <= ($xmlcount - 1); $i++) {

    echo "<div class=\"col-md-12 newsbox\">
           <img style=\"margin-bottom: 10px;\" width=\"100%\" src=\"images/news/".$xmldata[$i]['image']."\">
            <div class=\"newsboxtext\">
              <a href=\"showArticle.php?file=".$xmldata[$i]['file']. "\">
                <h2 class=\"mainheadline2\"> ".$xmldata[$i]['headline']. "</h2>
              </a>
              <a href=\"showArticle.php?file=".$xmldata[$i]['file']. "\">
                <button  style=\"margin-top:5px;\" type=\"button\" class=\"btn btn-sm btn-default\">+ Read More</button>
              </a>
            </div><hr class=\"linedivider\">
          </div>";

}

$file在本例中是句柄而不是数组。您可以在while循环$array[]=$file中创建一个数组;这应该行得通,试过了。现在我遇到了这样一个错误:无法重新声明日期\u compareFirst验证$file是一个数组并且是您所期望的。这个错误意味着函数已经被声明了,要么它已经在程序中的某个地方声明了,要么更可能,它在循环中?我确实在循环中声明了函数。我将其移动到循环下方,现在它显示了3篇主要文章,但没有按日期对它们进行排序。请确保date1-date2是正数,因为它使用intval,负数将导致所有文章都为0。您可能只需要执行反向date2-date1操作,从比较函数返回非整数值,例如float,这将导致回调返回值的内部转换为整数。因此,0.99和0.1之类的值都将转换为0的整数值,这将比较这些值是否相等。尝试使用此方法,但出现此错误。非法字符串偏移量“file”信息丰富,位于echo命令的末尾。请参阅编辑,我在其中更改了for循环并将$xmldata呈现为多维数组。感谢您的帮助。我尝试了上面的更改,现在得到了未定义的索引:dateseewedit。usort函数不喜欢新的多维数组。我甚至不得不重新定义$xmldata,并删除了匿名的“日期比较”函数,因为如果使用PHP5.3+,您不需要它。最后,由于我们将日期保留为字符串,所以strcmp用于订购。现在我得到一个未定义的偏移量错误。这段描述提取文本函数的代码会导致问题吗?函数extractText$array{ifcount$array
$r = 0;
$xml = new DOMDocument('1.0', 'UTF-8');

function extractText($tag) {
   foreach ($tag as $item) {
      $value = $item->nodeValue;
   }
   return $value;
}

while ($file = readdir($dh) and $fileCount < 3) {

    if (preg_match("/^..?$/", $file)) {
        continue;
    }

    $open = "./xml/".$file;
    $xml->load($open);

    //we need to pull out all the things from this file that we will need to 
    //build our links
    $xmldata = [];
    $xmldata[$r]['file'] = $file;

    $stat_array = $xml->getElementsByTagName("status");
    $xmldata[$r]['status'] = extractText($stat_array);

    $date_array = $xml->getElementsByTagName("date");
    $xmldata[$r]['date']= extractText($date_array);

    $ab_array = $xml->getElementsByTagName("abstract");
    $xmldata[$r]['abstract'] = extractText($ab_array);

    $h_array = $xml->getElementsByTagName("headline");
    $xmldata[$r]['headline'] = extractText($h_array);

    $img_array = $xml->getElementsByTagName("image");
    $xmldata[$r]['image'] = extractText($img_array);

    $lead_array = $xml->getElementsByTagName("para-intro");
    $xmldata[$r]['paraintro'] = extractText($lead_array);

    if ($status != "live") {
        continue;
    }

    $fileCount++;
    $r++;
}

// YYYY-MM-DD STRINGS STILL SORTS IN DAILY ORDER (I.E., '2015-01-01' < '2015-12-31')
usort($xmldata, function($a, $b) {
    return strcmp($a['date'], $b['date']);
});

$xmlcount = sizeof($xmldata[0]);

// ITERATE THROUGH LOOP EVERY NEEDED ITEM IN SORT ORDER OF ARRAY
for ($i = 0; $i <= ($xmlcount - 1); $i++) {

    echo "<div class=\"col-md-12 newsbox\">
           <img style=\"margin-bottom: 10px;\" width=\"100%\" src=\"images/news/".$xmldata[$i]['image']."\">
            <div class=\"newsboxtext\">
              <a href=\"showArticle.php?file=".$xmldata[$i]['file']. "\">
                <h2 class=\"mainheadline2\"> ".$xmldata[$i]['headline']. "</h2>
              </a>
              <a href=\"showArticle.php?file=".$xmldata[$i]['file']. "\">
                <button  style=\"margin-top:5px;\" type=\"button\" class=\"btn btn-sm btn-default\">+ Read More</button>
              </a>
            </div><hr class=\"linedivider\">
          </div>";

}