Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.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中对XML foreach进行排序_Php_Xml_Foreach_Simplexml - Fatal编程技术网

在PHP中对XML foreach进行排序

在PHP中对XML foreach进行排序,php,xml,foreach,simplexml,Php,Xml,Foreach,Simplexml,这是我的代码,它显示了我想要的结果,但是可以按顺序或顺序列出它们吗 $xml = simplexml_load_file('racing.xml'); foreach ($xml->sport[0]->event_path->event_path as $gameinfo): $description = $gameinfo->description; $getdate = $gameinfo

这是我的代码,它显示了我想要的结果,但是可以按顺序或顺序列出它们吗

    $xml = simplexml_load_file('racing.xml');

    foreach ($xml->sport[0]->event_path->event_path as $gameinfo):

    $description        =   $gameinfo->description;
    $getdate            =   $gameinfo->event['date'];
    $event_id           =   $gameinfo->event['id'];
    $date               =   substr($getdate,0,10);
代码

    <?=substr($description, -5)?>

留给我的变量是times,即:14:40、15:50:14:20:18:40等,但它们是按XML的顺序显示的,而不是按时间显示的


是否有一行代码可用于按日期变量对结果进行排序?

PHP具有非常有用的排序函数,能够定义用户比较函数。请参阅链接:

首先是一些改进代码的一般提示:

foreach ($xml->sport[0]->event_path->event_path as $gameinfo):
这是个坏主意。相反,我给自己做了一份礼物,并给自己一个新的变量(你可以稍后感谢我):

现在您需要对
$gameinfos
进行排序。这里的问题是它们是迭代器而不是数组。
uasort
函数(以及所有其他数组排序函数)对您没有任何帮助。幸运的是,您可以将迭代转换为数组:

$gameinfos = iterator_to_array($gameinfos, FALSE);
现在,
$gameinfos
是一个可以排序的数组。为此,要获取定义排序顺序的值(应该为其排序,
$gameinfos
),我假设是您在上面编写的时间
substr($description,-5)

$order = array();
foreach ($gameinfos as $game) 
    $order[] = substr($game->description, -5)
;

array_multisort($order, $gameinfos);

// $gameinfos are sorted now.

谢谢你的时间!我现在的代码是:

    $xml = simplexml_load_file('racing.xml');

    $gameinfos = $xml->sport[0]->event_path->event_path;
    foreach ($gameinfos as $gameinfo):

    $gameinfos = iterator_to_array($gameinfos, FALSE);

    $order = array();
    foreach ($gameinfos as $game) 
    $order[] = substr($game->description, -5) ;

    array_multisort($order, $gameinfos);

    // $gameinfos are sorted now.

    $description        =   $gameinfo->description;
    $getdate            =   $gameinfo->event['date'];
    $event_id           =   $gameinfo->event['id'];
    $date               =   substr($getdate,0,10);

不过,这只返回了一个结果,我想我在这一过程中犯了一些错误?

我正在绞尽脑汁想如何将其应用到我的代码中,不过,明天我会用新的眼光来看看!谢谢
    $xml = simplexml_load_file('racing.xml');

    $gameinfos = $xml->sport[0]->event_path->event_path;
    foreach ($gameinfos as $gameinfo):

    $gameinfos = iterator_to_array($gameinfos, FALSE);

    $order = array();
    foreach ($gameinfos as $game) 
    $order[] = substr($game->description, -5) ;

    array_multisort($order, $gameinfos);

    // $gameinfos are sorted now.

    $description        =   $gameinfo->description;
    $getdate            =   $gameinfo->event['date'];
    $event_id           =   $gameinfo->event['id'];
    $date               =   substr($getdate,0,10);