simpleXML的PHP排序问题
test.xml:simpleXML的PHP排序问题,php,simplexml,Php,Simplexml,test.xml: <?xml version="1.0"?> <props> <prop> <state statename="Mississippi"> <info> <code>a1</code> <location>Jackson</location> </info> <info> &l
<?xml version="1.0"?>
<props>
<prop>
<state statename="Mississippi">
<info>
<code>a1</code>
<location>Jackson</location>
</info>
<info>
<code>d2</code>
<location>Gulfport</location>
</info>
<info>
<code>g6</code>
<location>Hattiesburg</location>
</info>
</state>
<state statename="Texas">
<info>
<code>i9</code>
<location>Dallas</location>
</info>
<info>
<code>a7</code>
<location>Austin</location>
</info>
</state>
<state statename="Maryland">
<info>
<code>s5</code>
<location>Mount Laurel</location>
</info>
<info>
<code>f0</code>
<location>Baltimore</location>
</info>
<info>
<code>h4</code>
<location>Annapolis</location>
</info>
</state>
</prop>
</props>
test.php
// start the sortCities
function sortCities($a, $b){
return strcmp($a->location, $b->location);
}
// start the sortStates
function sortStates($t1, $t2) {
return strcmp($t1['statename'], $t2['statename']);
}
$props = simplexml_load_file('test.xml');
foreach ($props->prop as $prop) {
$sortedStates = array();
foreach($prop->state as $states) {
$sortedStates[] = $states;
}
usort($sortedStates, "sortStates"); // finish the sortStates
/* --- */
echo '<pre>'."\n";
print_r($sortedStates);
echo '</pre>'."\n";
/* --- */
foreach ($prop->children() as $stateattr) { // this doesn't do it
//foreach($sortedStates as $hotel => @attributes){ // blargh!
if(isset($stateattr->info)) {
$statearr = $stateattr->attributes();
echo '<optgroup label="'.$statearr['statename'].'">'."\n";
$options = array();
foreach($stateattr->info as $info) {
$options[] = $info;
}
usort($options, "sortCities"); // finish the sortCities
foreach($options as $stateattr => $info){
echo '<option value="'.$info->code.'">'.$info->location.'</option>'."\n";
}
echo '</optgroup>'."\n";
} else {
//empty nodes don't do squat
}
}
}
?>
打印出:
// start the sortCities
function sortCities($a, $b){
return strcmp($a->location, $b->location);
}
这:
加上这部分代码:
<optgroup label="Maryland">
<option value="h4">Annapolis</option>
<option value="f0">Baltimore</option>
<option value="s5">Mount Laurel</option>
</optgroup>
<optgroup label="Mississippi">
<option value="d2">Gulfport</option>
<option value="g6">Hattiesburg</option>
<option value="a1">Jackson</option>
</optgroup>
<optgroup label="Texas">
<option value="a7">Austin</option>
<option value="i9">Dallas</option>
</optgroup>
第二个函数在数组中按属性(statename)排序,但是,将这两个函数组合起来,或者更确切地说,将它们嵌套,以便按字母顺序对州和城市进行排序,这让我感到困惑
@人造的 谢谢你的回复。它的嵌套方式似乎有道理。问题是,我的服务器都没有运行PHP5.3。因此,泛型函数正在抛出错误。我本应该提到这一点,但没有考虑。他们正在运行5.2。我一直在试图恢复脚本,但被一个部分卡住了
$doc = new SimpleXMLElement($xml);
$states = get_object_vars($doc->prop->children());
$states = $states["state"];
usort($states, function($t1, $t2) { return strcmp($t1['statename'], $t2['statename']); });
array_walk($states,
function (&$state) {
$state = get_object_vars($state);
array_walk($state["info"],
function (&$el) {
$el = get_object_vars($el);
}
);
usort($state["info"],
function($a, $b) { return strcmp($a["location"], $b["location"]); }
);
}
);
注释掉的部分以数组_walk开始。我不知道如何重写“函数(&$state)”,而不让下一行消失。我的方法是将SimpleXMLElement对象转换为数组:
foreach ($sortedStates as $stateattr)
改变这个
$tours = iterator_to_array($result->tour, false);
对此
$tourDates = array_map(
function($tour) {return trim($tour->next_bookable_date); },
$tours
);
// sort $tours array by dates, highest to lowest
$success = array_multisort($tourDates, SORT_DESC, $tours);
成功了
我可以发誓我已经试过了。您想根据某个属性或元素值对
simplexmlement
s列表进行排序。通常,您可以将列表转换为一个数组,然后对该数组应用排序
在PHP中,有一个函数对此很有帮助。它可以将simplexml中非常神奇的东西变成一个更“固定”的数组
让我们假设您在$result->tour
中的tour元素。默认情况下,simplexml在这里返回一个迭代器,这是您无法直接排序的。让我们将其转换为一个数组:
$tours = iterator_to_array($result->tour, false);
$tourDates = array_map(
function($tour) {return trim($tour->next_bookable_date); },
$tours
);
// sort $tours array by dates, highest to lowest
$success = array_multisort($tourDates, SORT_DESC, $tours);
foreach ($tours as $tour) {
...
}
现在,$tours
变量是一个数组,包含每个
元素作为simplexmlement
。现在可以使用数组函数对该数组进行排序。我通常建议将这些作为一个良好的起点:
就这样。整个代码一目了然:
不要让我们阅读代码来理解您想要做什么。以尽可能简洁的方式告诉我们您想要做什么,然后向我们展示代码。然后我们可以在上下文中阅读它,这比从代码中猜测要容易得多。“我遇到的问题是呼应并组合这两个函数,以便让它自动对其中的州和城市进行排序,并形成所需的optgroup。”我这样做了。谢谢你玩。哪两个功能?代码段中没有函数。这些函数应该做什么?自动分拣?与非自动排序相反?而且咆哮无助于你的事业。你应该举例说明你想要实现的目标,而不是你实际得到的目标。
$doc = new SimpleXMLElement($xml);
$states = get_object_vars($doc->prop->children());
$states = $states["state"];
usort($states, function($t1, $t2) { return strcmp($t1['statename'], $t2['statename']); });
array_walk($states,
function (&$state) {
$state = get_object_vars($state);
array_walk($state["info"],
function (&$el) {
$el = get_object_vars($el);
}
);
usort($state["info"],
function($a, $b) { return strcmp($a["location"], $b["location"]); }
);
}
);
foreach ($prop->children() as $stateattr)
foreach ($sortedStates as $stateattr)
$tours = iterator_to_array($result->tour, false);
$tourDates = array_map(
function($tour) {return trim($tour->next_bookable_date); },
$tours
);
// sort $tours array by dates, highest to lowest
$success = array_multisort($tourDates, SORT_DESC, $tours);
$tours = iterator_to_array($result->tour, false);
$tourDates = array_map(
function($tour) {return trim($tour->next_bookable_date); },
$tours
);
// sort $tours array by dates, highest to lowest
$success = array_multisort($tourDates, SORT_DESC, $tours);
foreach ($tours as $tour) {
...
}