Magento simplexml的反向输出

Magento simplexml的反向输出,magento,simplexml,usort,array-reverse,Magento,Simplexml,Usort,Array Reverse,无法反转输出,请尝试使用数组\ U reverse和usort 我正在尝试使用magmi数据泵将产品从XML导入Magento,工作正常,但我需要以相反的顺序输出,以便Magento将简单产品与可配置产品链接起来 有什么想法吗 $xml = simplexml_load_file("23.xml") or die("Error: Cannot create object"); foreach($xml->wapiitems->record as $book) { $item =

无法反转输出,请尝试使用数组\ U reverse和usort

我正在尝试使用magmi数据泵将产品从XML导入Magento,工作正常,但我需要以相反的顺序输出,以便Magento将简单产品与可配置产品链接起来

有什么想法吗

$xml = simplexml_load_file("23.xml") or die("Error: Cannot create object");

foreach($xml->wapiitems->record as $book) {

$item = $book->fields->itemno;
if (strlen($item) <= 6) {
$type = "configurable";
$ca = "color,size";}
else {
$type = "simple";
$ca = "";}

$newProductData = array(
        'sku'          => (string)$book->fields->itemno,        // name
        'type'           => (string)$type,        // sku
        'color' => (string)$book->subtables->descriptions->record->fields->variant1name,    // special price        
        'size'         => (string)$book->subtables->descriptions->record->fields->variant3name,     // price
        'attribute_set' => 'Default',            // attribute_set
        'store'         => 'admin',            
        'name'   => (string)$book->subtables->descriptions->record->fields->description,        // full description
        'configurable_attributes' => (string)$ca    // short description

);

//$dp->ingest($newProductData);
echo "</br>";
print_r ($newProductData);
$newProductData=null;    //clear memory
unset($newProductData); //clear memory
}
unset($xml);

$dp->endImportSession();   // end import
但我需要这个:

Array ( [sku] => 903490101006 [type] => simple [color] => Black [size] => 6 [attribute_set] => Default [store] => admin [name] => Q-Irine Cover [configurable_attributes] => )
Array ( [sku] => 903490101005 [type] => simple [color] => Black [size] => 5 [attribute_set] => Default [store] => admin [name] => Q-Irine Cover [configurable_attributes] => ) 
Array ( [sku] => 903490101004 [type] => simple [color] => Red [size] => 4 [attribute_set] => Default [store] => admin [name] => Q-Irine Cover [configurable_attributes] => ) 
Array ( [sku] => 90349 [type] => configurable [color] => [size] => [attribute_set] => Default [store] => admin [name] => [configurable_attributes] => color,size ) 
根据您的示例,不确定如何链接简单产品和可配置产品,但假设您的问题所需的只是在简单产品之后导入可配置产品,您可以执行以下操作:

通过更改$newProductData=array将产品记录从XML中提取出来后,创建产品记录的中间数组。。。到$newProductData[]=数组

现在使用类似的方法按产品类型对中间数组进行排序:

usort($newProductData, function($a, $b)
{
    if ($a['type'] == 'configurable' && $b['type'] == 'simple') {
        return 1;
    } else if ($a['type'] == 'simple' && $b['type'] == 'configurable') {
        return -1;
    } else {
        return strnatcmp($a['sku'], $b['sku']);
    }
});
最后,迭代排序数组并完成导入:

foreach ($newProductData as $data) {
    $dp->ingest($data);
}
foreach ($newProductData as $data) {
    $dp->ingest($data);
}