排序对象(SimpleXMLElement)php
我正试图找到一种方法,从SimpleXMLElement对数组进行排序。我想根据事件开始时间进行排序。我还想将房间ID作为一个单独的过程进行排序。当前数组按对象(SimpleXMLElement)#排序。以下是var_dump($array): 我试过usort和asort,但两种方法都没有成功 usort方法:排序对象(SimpleXMLElement)php,php,sorting,simplexml,usort,Php,Sorting,Simplexml,Usort,我正试图找到一种方法,从SimpleXMLElement对数组进行排序。我想根据事件开始时间进行排序。我还想将房间ID作为一个单独的过程进行排序。当前数组按对象(SimpleXMLElement)#排序。以下是var_dump($array): 我试过usort和asort,但两种方法都没有成功 usort方法: function sortByTime($a, $b){ $a = strtotime($array->event_start_dt); $b = strtoti
function sortByTime($a, $b){
$a = strtotime($array->event_start_dt);
$b = strtotime($array->event_start_dt);
if ($a==$b) return 0;
return ($a < $b) ?-1 : 1;
}
usort($arrTimes, 'sortByTime');
var_dump($arrTimes);
请求打印\u r:
/**
* function xml2array
*
* This function is part of the PHP manual.
*
* The PHP manual text and comments are covered by the Creative Commons
* Attribution 3.0 License, copyright (c) the PHP Documentation Group
*
* @author k dot antczak at livedata dot pl
* @date 2011-04-22 06:08 UTC
* @link http://www.php.net/manual/en/ref.simplexml.php#103617
* @license http://www.php.net/license/index.php#doc-lic
* @license http://creativecommons.org/licenses/by/3.0/
* @license CC-BY-3.0 <http://spdx.org/licenses/CC-BY-3.0>
*/
function xml2array ( $xmlObject, $out = array () )
{
foreach ( (array) $xmlObject as $index => $node )
$out[$index] = ( is_object ( $node ) ) ? xml2array ( $node ) : $node;
return $out;
}
我将使用这个函数(php.net中的示例函数)将其转换为一个数组。但请注意,这不会对XML进行排序,而是对新数组进行排序
$arrTimes = xml2array(YourSimpleXMLElement);
usort($arrTimes, 'sortByTime');
然后在新阵列上使用原始usort函数
$arrTimes = xml2array(YourSimpleXMLElement);
usort($arrTimes, 'sortByTime');
使用
<?php
$myarray=array(
0 => array
(
'dateTime' => '2013-12-02T10:00:00-08:00',
'chanl1' => '20.10',
'chanl2' => '45.4',
'chanl3' => '',
),
1 => array
(
'dateTime' => '2013-12-02T11:00:00-08:00',
'chanl1' => '20.11',
'chanl2' => '45.4',
'chanl3' => '',
),
2 => array
(
'dateTime' => '2013-12-02T12:00:00-08:00',
'chanl1' => '20.12',
'chanl2' => '33.8',
'chanl3' => '',
),
3 => array
(
'dateTime' => '2013-12-02T09:00:00-08:00',
'chanl1' => '20.9',
'chanl2' => '33.9',
'chanl3' => ''
));
foreach($myarray as $c=>$key) {
$dateTime[] = $key['dateTime'];
}
array_multisort($dateTime,SORT_ASC,SORT_STRING,$myarray);
echo "<pre>";
print_r($myarray);
?>
在对数据进行排序之前,需要创建一个数组,其中包含要排序的单独项目作为其值。从调试输出来看,这些是输入XML中的多个
节点,它们是这些示例中由$array
/$this->data
表示的元素的子元素(不管它是否是文档的根,SimpleXML没有文档对象)
<?php
$myarray=array(
0 => array
(
'dateTime' => '2013-12-02T10:00:00-08:00',
'chanl1' => '20.10',
'chanl2' => '45.4',
'chanl3' => '',
),
1 => array
(
'dateTime' => '2013-12-02T11:00:00-08:00',
'chanl1' => '20.11',
'chanl2' => '45.4',
'chanl3' => '',
),
2 => array
(
'dateTime' => '2013-12-02T12:00:00-08:00',
'chanl1' => '20.12',
'chanl2' => '33.8',
'chanl3' => '',
),
3 => array
(
'dateTime' => '2013-12-02T09:00:00-08:00',
'chanl1' => '20.9',
'chanl2' => '33.9',
'chanl3' => ''
));
foreach($myarray as $c=>$key) {
$dateTime[] = $key['dateTime'];
}
array_multisort($dateTime,SORT_ASC,SORT_STRING,$myarray);
echo "<pre>";
print_r($myarray);
?>
您的print\u r
和var\u dump
输出显示您当前没有这样的数组,只有一个SimpleXML对象:
- 您的第一个示例显示了
以var_dump($array)
-忽略object(simplexmlement)#275(1){
这个词,这就是array
呈现对象内部的方式var_dump
- 后来,您有了一个
开头的print\r($array);
——但这只是因为您已经将实际数据包装在上面一行的单个元素数组中(array(1){
),并且一个元素($array=array($this->data);
)显示为$array[0]
对象(simplexmlement)#280(1){…
foreach
循环,以获得最大的代码可读性,尽管有“更聪明”的解决方案
一旦你有了一个可排序的列表,你就需要一个回调函数来比较它的两个参数。usort
变量$array
;需要比较的值是函数的参数,您调用了$a
和$b
——具体来说,您希望将strottime($a->event_start_dt)
与strottime($b->event_start_dt)
进行比较
您还可以使函数更简单,因为它遵循了一个常见的错误概念,即回调的返回值应该是-1
、0
或1
。事实上,它可以是任何整数,只有它的符号才重要-返回-42
与返回-999
具有相同的效果,即placi在结果数组中的$b
之前的ng项$a
我不能简单地给出一个经过测试的示例,因为您没有提供底层XML来重现您的输入(例如echo$this->data->asXML();
),但我将采取的基本方法是:
Array
(
[0] => Array
(
[dateTime] => 2013-12-02T09:00:00-08:00
[chanl1] => 20.9
[chanl2] => 33.9
[chanl3] =>
)
[1] => Array
(
[dateTime] => 2013-12-02T10:00:00-08:00
[chanl1] => 20.10
[chanl2] => 45.4
[chanl3] =>
)
[2] => Array
(
[dateTime] => 2013-12-02T11:00:00-08:00
[chanl1] => 20.11
[chanl2] => 45.4
[chanl3] =>
)
[3] => Array
(
[dateTime] => 2013-12-02T12:00:00-08:00
[chanl1] => 20.12
[chanl2] => 33.8
[chanl3] =>
)
)
//从一个空数组开始,将我们感兴趣的所有项目添加到该数组中
$sortable_array=array();
//在SimpleXML对象$this->data的所有子对象上循环
//看http://php.net/manual/en/simplexml.examples-basic.php
foreach($this->data->reservation as$reservation\u节点)
{
//将单个节点添加到数组中
$sortable_数组[]=$reservation_节点;
}
//现在让我们整理一下用于排序的回调函数
//这也可以是直接传递给usort的匿名函数
函数排序\回调\事件\开始($a,$b)
{
//$a和$b都是$sortable_数组中的项,因此
//我们希望每个节点都有一个子节点
//叫
//如果我们将这两个日期都转换为Unix时间戳,则有两个整数
//要进行比较,只需简单的减法即可得到所需的结果
//在中记录的0的值http://php.net/usort
返回
strtotime((字符串)$a->event\u start\u dt)
-
strotime((字符串)$b->事件\u开始\u dt);
}
//现在,我们有了进行实际排序所需的一切
usort($sortable_array,'sort_callback_event_start');
//$sortable_数组现在已按需要排序!:D
//请注意,其中的项仍然是SimpleXML对象,
//所以你仍然需要访问它们的属性来做一些有用的事情
//例如,一些HTML输出,其名称按开始日期顺序列出:
回声';
Array
(
[0] => Array
(
[dateTime] => 2013-12-02T09:00:00-08:00
[chanl1] => 20.9
[chanl2] => 33.9
[chanl3] =>
)
[1] => Array
(
[dateTime] => 2013-12-02T10:00:00-08:00
[chanl1] => 20.10
[chanl2] => 45.4
[chanl3] =>
)
[2] => Array
(
[dateTime] => 2013-12-02T11:00:00-08:00
[chanl1] => 20.11
[chanl2] => 45.4
[chanl3] =>
)
[3] => Array
(
[dateTime] => 2013-12-02T12:00:00-08:00
[chanl1] => 20.12
[chanl2] => 33.8
[chanl3] =>
)
)
foreach($reservation\u节点的可排序数组)
{
回显“”,(字符串)$reservation\u node->event\u name“ ”;
Array
(
[0] => Array
(
[dateTime] => 2013-12-02T09:00:00-08:00
[chanl1] => 20.9
[chanl2] => 33.9
[chanl3] =>
)
[1] => Array
(
[dateTime] => 2013-12-02T10:00:00-08:00
[chanl1] => 20.10
[chanl2] => 45.4
[chanl3] =>
)
[2] => Array
(
[dateTime] => 2013-12-02T11:00:00-08:00
[chanl1] => 20.11
[chanl2] => 45.4
[chanl3] =>
)
[3] => Array
(
[dateTime] => 2013-12-02T12:00:00-08:00
[chanl1] => 20.12
[chanl2] => 33.8
[chanl3] =>
)
)
}
回声';
Array
(
[0] => Array
(
[dateTime] => 2013-12-02T09:00:00-08:00
[chanl1] => 20.9
[chanl2] => 33.9
[chanl3] =>
)
[1] => Array
(
[dateTime] => 2013-12-02T10:00:00-08:00
[chanl1] => 20.10
[chanl2] => 45.4
[chanl3] =>
)
[2] => Array
(
[dateTime] => 2013-12-02T11:00:00-08:00
[chanl1] => 20.11
[chanl2] => 45.4
[chanl3] =>
)
[3] => Array
(
[dateTime] => 2013-12-02T12:00:00-08:00
[chanl1] => 20.12
[chanl2] => 33.8
[chanl3] =>
)
)
您必须使用json encode decode将frist in转换为xml到数组
$xml\u array=json\u decode(json\u encode((array)$xml),TRUE)
你将得到数组列表…你可以使用strotime函数根据日期排序。usort()应该在单独的数组上工作,你到底尝试了什么?要记住的关键是
simplexmlement
不是数组,它是一个对象(而且var\u dump
非常容易误导)。它不能直接排序,因为它表示XML文件的实际结构,而不仅仅是其数据的副本。我添加了一个我尝试过的usort方法。我无法通过$array['reservation'][$I]['event\u start\u dt']获取数据但我可以通过$array->event\u start\u dt获得它。请参阅本文中接受的答案:应用此方法可能会重复get错误。参数#4应该是一个数组或此行的排序标志:array\u multisort($dateTime,sort\u ASC,sort\u STRING,$rez->RESERATION);@sloga您的预订是否为数组??我是说$rez->reservationI我不完全理解这一点…我得到$a=strotime的“未定义索引”(
// Start with an empty array, and add all the items we're interested in to it
$sortable_array = array();
// Loop over all <reservation> children of the SimpleXML object $this->data
// See http://php.net/manual/en/simplexml.examples-basic.php
foreach ( $this->data->reservation as $reservation_node )
{
// Add the individual node to our array
$sortable_array[] = $reservation_node;
}
// Now let's sort out the callback function for the sorting
// This could also be an anonymous function passed directly to usort
function sort_callback_event_start($a, $b)
{
// $a and $b are both items in our $sortable_array, and therefore
// <reservation> nodes which we expect to each have a child
// called <event_start_dt>
// If we convert both dates to Unix timestamps, we have two integers
// to compare, and a simple subtraction gives the desired result
// of <0, 0, or >0 as documented at http://php.net/usort
return
strtotime((string)$a->event_start_dt)
-
strtotime((string)$b->event_start_dt);
}
// Now, we have everything we need to do the actual sorting
usort($sortable_array, 'sort_callback_event_start');
// $sortable_array is now sorted as desired! :D
// Note that the items within it are still SimpleXML objects,
// so you still need to access their properties to do something useful
// e.g. some HTML output with the names listed in order of their start date:
echo '<ol>';
foreach ( $sortable_array as $reservation_node )
{
echo '<li>', (string)$reservation_node->event_name, '</li>';
}
echo '</ol>';