从 XML(简单 XML 解析器)导入数据时重复 PHP

Repetion while importing data from XML ( Simple XML Parser ) PHP

我正在尝试从 XML 中提取数据,如下所示

<list last_update="2018-12-11 17:37:18" listing_count="196">
    <property last_update="2018-12-10 15:02:21">
    <agent>
    <name>
    <![CDATA[ G. G. ]]>
    </name>
    <email>sales29@shadesproperties.ae</email>
    <phone>+971 50 355 4416</phone>
    <license_no>38729</license_no>
    </agent>
    <parking>1</parking>
    <photo>
    <url last_update="2018-10-07 16:31:14" watermark="Yes">
    https://s3-ap-southeast-1.amazonaws.com/mycrm-pro-accounts-v2/property/full/1300/OWWFGpAN3l4tF5eU.jpeg
    </url>
    <url last_update="2018-10-07 16:31:14" watermark="Yes">
    https://s3-ap-southeast-1.amazonaws.com/mycrm-pro-accounts-v2/property/full/1300/ZNBpQDrbx7fSURP6.jpeg
    </url>
    <url last_update="2018-10-07 16:31:14" watermark="Yes">
    https://s3-ap-southeast-1.amazonaws.com/mycrm-pro-accounts-v2/property/full/1300/DKDkGl1DXb5Ec75E.jpeg
    </url>
    <url last_update="2018-10-07 16:31:14" watermark="Yes">
    https://s3-ap-southeast-1.amazonaws.com/mycrm-pro-accounts-v2/property/full/1300/aMUA4rFzFRnr0jru.jpeg
    </url>
    <url last_update="2018-10-07 16:31:14" watermark="Yes">
    https://s3-ap-southeast-1.amazonaws.com/mycrm-pro-accounts-v2/property/full/1300/70UFXHqM7OAo3f5h.jpeg
    </url>
    <url last_update="2018-10-07 16:31:14" watermark="Yes">
    https://s3-ap-southeast-1.amazonaws.com/mycrm-pro-accounts-v2/property/full/1300/sELG0dzNyOxmwlFE.jpeg
    </url>
    <url last_update="2018-10-07 16:31:14" watermark="Yes">
    https://s3-ap-southeast-1.amazonaws.com/mycrm-pro-accounts-v2/property/full/1300/x2S9Q0BvwT1fZUyb.jpeg
    </url>
    <url last_update="2018-10-07 16:31:14" watermark="Yes">
    https://s3-ap-southeast-1.amazonaws.com/mycrm-pro-accounts-v2/property/full/1300/s9MDwWwYKeloiibS.jpeg
    </url>
    <url last_update="2018-10-07 16:31:14" watermark="Yes">
    https://s3-ap-southeast-1.amazonaws.com/mycrm-pro-accounts-v2/property/full/1300/SahPWXAGL0dAMpMg.jpeg
    </url>
    <url last_update="2018-12-10 14:59:07" watermark="Yes">
    https://s3-ap-southeast-1.amazonaws.com/mycrm-pro-accounts-v2/property/full/1300/iAuYR3KfjNRIAJkk.jpeg
    </url>
    </photo>
    <geopoints>55.34016973,25.2242947</geopoints>
    </property>
</list>

我尝试将数据加载到导入器(由 Perch CMS 提供)以导入为 collection 项。

问题是每个 collection 项都具有相同的图像集。

看看我用来提取数据的 PHP 代码。

<?php
include('perch/runtime.php');
$images = array();
$location = array();

$API      = new PerchAPI(1.0, 'my_importer');
$Importer = $API->get('CollectionImporter');
$Importer->set_collection('property');
$Template = $API->get('Template');
$Template->set('content/text', 'content');
$Importer->set_template($Template);
$Importer->empty_collection();
$xml=simplexml_load_file("https://shades.mycrm.com/feed/privatesite/0744a0be5fac33c3b3e69c6b26e97d1e") or die("Error: Cannot create object");

foreach($xml->children() as $propertylisting) {    
foreach($propertylisting->photo->children() as $image_url) {
$images[] = $image_url->__toString(); 
}
    $location[] = $propertylisting->community->__toString(); 
    try {
    $Importer->add_item([
        'reference_number'   =>  $propertylisting->reference_number  ,
        'description_en'          =>  $propertylisting->description_en  ,
        'title_property'            =>  $propertylisting->title_en  ,
        'property_image_1'    =>  $images[0],
         'property_image_2'   =>  $images[1],
        'property_image_3'    =>  $images[2],
        'property_image_4'    =>  $images[3],
        'property_image_5'    =>  $images[4],
        'property_image_6'    =>  $images[5],
        
    ]);    
} catch (Exception $e) {
    die('Error: '.$e->getMessage());
}
} 
$result = array_unique($location);
?> 

如何将图像唯一地添加到导入器中。

我看到发生的一个问题是你永远不会在每次循环时清除 $images 数组,所以这意味着不是为这个列表建立一个图像列表,它们会累积对于所有列表。如此简单地为每个 属性 列表重置数组...

foreach($xml->children() as $propertylisting) {    
    // Reset array
    $images = array();
    foreach($propertylisting->photo->children() as $image_url) {
        $images[] = $image_url->__toString(); 
    }