Arrays simplexml中的排序和数组

Arrays simplexml中的排序和数组,arrays,sorting,simplexml,Arrays,Sorting,Simplexml,我有一个productxml,有多个不同价格的卖家。我需要在遍历simplexml对象并将其放入数组后,以某种方式对数组进行排序 循环浏览xml文件: foreach($item->Offers->Offer as $offer) { $trackerprices[]['price'] = $offer->Price; $trackerprices[]['id_seller'] = $offer->Seller->Id; } 因此,当我var_dum

我有一个productxml,有多个不同价格的卖家。我需要在遍历simplexml对象并将其放入数组后,以某种方式对数组进行排序

循环浏览xml文件:

    foreach($item->Offers->Offer as $offer) {

$trackerprices[]['price'] = $offer->Price;
$trackerprices[]['id_seller'] = $offer->Seller->Id;


}
因此,当我var_dump$trackerPrice时,它会输出:

array(18) { [0]=> array(1) { ["price"]=> object(SimpleXMLElement)#22 (1) { [0]=> string(4) "10.0" } } [1]=> array(1) { ["id_seller"]=> object(SimpleXMLElement)#26 (1) { [0]=> string(16) "1001004002814931" } } [2]=> array(1) { ["price"]=> object(SimpleXMLElement)#16 (1) { [0]=> string(4) "8.29" } } [3]=> array(1) { ["id_seller"]=> object(SimpleXMLElement)#28 (1) { [0]=> string(6) "187216" } } [4]=> array(1) { ["price"]=> object(SimpleXMLElement)#24 (1) { [0]=> string(3) "9.0" } } [5]=> array(1) { ["id_seller"]=> object(SimpleXMLElement)#30 (1) { [0]=> string(6) "441404" } } [6]=> array(1) { ["price"]=> object(SimpleXMLElement)#25 (1) { [0]=> string(4) "9.75" } } [7]=> array(1) { ["id_seller"]=> object(SimpleXMLElement)#32 (1) { [0]=> string(5) "25430" } } [8]=> array(1) { ["price"]=> object(SimpleXMLElement)#27 (1) { [0]=> string(4) "9.95" } } [9]=> array(1) { ["id_seller"]=> object(SimpleXMLElement)#34 (1) { [0]=> string(6) "150362" } } [10]=> array(1) { ["price"]=> object(SimpleXMLElement)#29 (1) { [0]=> string(3) "8.0" } } [11]=> array(1) { ["id_seller"]=> object(SimpleXMLElement)#36 (1) { [0]=> string(6) "502339" } } [12]=> array(1) { ["price"]=> object(SimpleXMLElement)#31 (1) { [0]=> string(3) "9.0" } } [13]=> array(1) { ["id_seller"]=> object(SimpleXMLElement)#38 (1) { [0]=> string(6) "117478" } } [14]=> array(1) { ["price"]=> object(SimpleXMLElement)#33 (1) { [0]=> string(4) "15.0" } } [15]=> array(1) { ["id_seller"]=> object(SimpleXMLElement)#40 (1) { [0]=> string(6) "153058" } } [16]=> array(1) { ["price"]=> object(SimpleXMLElement)#35 (1) { [0]=> string(3) "6.5" } } [17]=> array(1) { ["id_seller"]=> object(SimpleXMLElement)#42 (1) { [0]=> string(6) "402391" } } } 
不,我需要一种按ASC价格对输出进行排序的方法

我被困在这里了,有人能帮我吗


提前谢谢

首先,您可能希望使用以下结构:

foreach($item->Offers->Offer as $offer) {
  $trackerprices[] = array('price' => (float)$offer->Price, 
                           'id_seller' => (string)$offer->Seller->Id);
}
这样,您可以将价格和相应的卖家id保持在一起。现在你有了一个价格卖方对数组,你需要它按照每对的价格字段进行排序。你可以用它。这只是一种方法,可能会有更有效的解决方案

foreach($trackerprices as $tp) {
     $sort[] = $tp['price'];
}
array_multisort($sort, SORT_ASC, $trackerprices);
总之,它还有3行代码:

$sort = array();
foreach($item->Offers->Offer as $offer) {
  $trackerprices[] = array('price' => (float)$offer->Price, 
                           'id_seller' => (string)$offer->Seller->Id);
  $sort[] = $tp['price'];
}
array_multisort($sort, SORT_ASC, $trackerprices);
我还没有测试过,但你明白了,我希望它能有所帮助