我的问题是,我从有价值的
更改了我的约会$date
进入一个名为
的数组$sortDate
现在,我想获取数组,并按日期排序。我的排序功能看起来像这样。
function sortArray($data, $field) {
if (!is_array($field))
$field = array($field);
usort($data, function($a, $b) use($field) {
$retval = 0;
foreach ($field as $fieldname) {
if ($retval == 0)
$retval = strnatcmp($a[$fieldname], $b[$fieldname]);
}
return $retval;
});
return array_reverse($data);
}
现在,这里是我在开头谈到的贵重物品的定义。
$xml = simplexml_load_file('http://www.hokejbal.cz/xml/2017-2018/shedules/227.xml');
foreach ($xml->SKUPINA as $skupina) {
if ($skupina['id_sku'] != 1613)
continue;
foreach ($skupina->UTKANI as $utkani) {
$away = $utkani->AWAY;
$date = new DateTime($utkani->DATUM);
$sortDate = array();
$sortDate[] = $utkani->DATUM;
print_r($sortDate); //this is just control line, yes, it is array.
现在,我想对日期进行排序......所以我写了这个
foreach ($utkani->DATUM as $item) {
$sortDate[] = array(
'datum' => $item->DATUM
);
}
if (isset($sortDate)) {
$sortDate = sortArray($sortDate, array('datum'));
所以,现在我很重要,当元素应该按照我眼中的$ date排序时,所以我准备写这样的东西
<tr>
<td nowrap><?=$sortDate->format ('m.d.y') ; ?></td>
</tr>
但是在这一点上,我只得到错误调用...
致命错误:未捕获错误:在C:\ xampp \ htdocs \ SKKelti2008 \ Tabulky_rozpisy_statistiky \ A \ page-rozpis-1718.php中调用数组上的成员函数格式():堆栈跟踪:#0 {main}扔进了 第62行的C:\ xampp \ htdocs \ SKKelti2008 \ Tabulky_rozpisy_statistiky \ A \ page-rozpis-1718.php
完成后,这只是我的完整代码,因此您可以了解表格的内容。解决问题并不重要,但它可以提供帮助。
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<div class="svazTabulky">
<TABLE>
<TR>
<th colspan="9">1. Národní hokejbalová liga 2017/2018</th>
</TR>
<TR>
<TD nowrap>DATUM</TD>
<TD nowrap>ČAS</TD>
<TD nowrap>MÍSTO</TD>
<TD nowrap>DOMÁCÍ</TD>
<TD nowrap>HOSTÉ</TD>
<TD nowrap>Výsledek</TD>
</tr>
<?php
function sortArray($data, $field) {
if (!is_array($field))
$field = array($field);
usort($data, function($a, $b) use($field) {
$retval = 0;
foreach ($field as $fieldname) {
if ($retval == 0)
$retval = strnatcmp($a[$fieldname], $b[$fieldname]);
}
return $retval;
});
return array_reverse($data);
}
// put your code here
$xml = simplexml_load_file('http://www.hokejbal.cz/xml/2017-2018/shedules/227.xml');
foreach ($xml->SKUPINA as $skupina) {
if ($skupina['id_sku'] != 1613)
continue;
foreach ($skupina->UTKANI as $utkani) {
$away = $utkani->AWAY;
$date = new DateTime($utkani->DATUM);
$sortDate = array();
foreach ($utkani->DATUM as $item) {
$sortDate[] = array(
'datum' => $item->DATUM
);
}
if (isset($sortDate)) {
$sortDate = sortArray($sortDate, array('datum'));
print_r($sortDate);
foreach ($utkani->HOME as $home) {
if ($home['kod_cmshb']!=3202007 and $away['kod_cmshb']!=3202007) continue;
?>
<tr <?php if($utkani['kod_cmshb']==3202007){ ?> class="beroun" <?php } ?>>
<td nowrap><?=$sortDate->format ('m.d.y') ; ?></td>
<td nowrap><?=$utkani->CAS ?></td>
<td nowrap><?=$utkani->MISTO ?></td>
<td nowrap><?=$utkani->HOME ?></td>
<td nowrap><?=$utkani->AWAY ?></td>
<td nowrap><?=$utkani->GH ?>:<?=$utkani->GA ?></td>
</tr>
<?php
}
}
}
break;
}
?>
</TABLE>
<p>Data převzata z <a href="http://hokejbal.cz"> hokejbal.cz</a></p>
</div>
</div><!-- #content .site-content -->
</div><!-- #primary .content-area -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
}
}
}
答案 0 :(得分:0)
如果你跑:
var_dump($sortDate);
将导致:
array(1) {
[0]=>
array(1) {
["datum"]=>
object(SimpleXMLElement)#7 (0) {
}
}
}
在这种情况下,$sortDate->format ('m.d.y')
将无效,因为您希望在阵列上运行format方法。
您声明了一个$date
变量,但您不在此代码中使用它。
$date = new DateTime($utkani->DATUM);
也许您在寻找$date->format ('m.d.y') ;
?