如何循环遍历类似命名的xml项目php

时间:2018-02-19 03:05:39

标签: php xml

我正在使用XML提要,在对象内部,它列出了多个项目,其中包含我想要访问的图片。我无法弄清楚如何遍历每个yachtPic而无需手动添加数字,对于某些Feed,pic数字会更高或更低。我知道我可能不得不用strpos()函数做一些事情,但我不确定如何尝试访问xml对象。

我曾经使用xml打印简单的xml对象:https://pastebin.com/vk1FW1i8

List<Student>

代码我正在尝试使用

 SimpleXMLElement Object
 (
     [yacht] => SimpleXMLElement Object
    (
            [yachtId] => SimpleXMLElement Object
            (
            )

        [yachtName] => SimpleXMLElement Object
            (
            )

        [yachtLogo] => SimpleXMLElement Object
            (
            )

        [yachtType] => Cat
        [yachtLength] => 77
        [yachtPowerCat] => N
        [sizeFeet] => 77.0 Ft
        [sizeMeter] => 23.0 m
        [yachtBeam] => 34
        [yachtDraft] => 6
        [yachtUnits] => Feet
        [yachtPax] => 10
        [yachtCabins] => 5
        [yachtKing] => 3
        [yachtQueen] => 2
        [yachtSingleCabins] => 0
        [yachtDoubleCabins] => 0
        [yachtTwinCabins] => 0
        [yachtPullmanCabins] => 0
        [yachtRefit] => SimpleXMLElement Object
            (
            )

        [yachtHelipad] => SimpleXMLElement Object
            (
            )

        [yachtJacuzzi] => SimpleXMLElement Object
            (
            )

        [yachtAc] => SimpleXMLElement Object
            (
            )

        [yachtPrefPickup] => SimpleXMLElement Object
            (
            )

        [yachtOtherPickup] => SimpleXMLElement Object
            (
            )

        [yachtTurnAround] => SimpleXMLElement Object
            (
            )

        [yachtYearBuilt] => 2016
        [yachtBuilder] => SimpleXMLElement Object
            (
            )

        [yachtBrokerWeb] => SimpleXMLElement Object
            (
            )

        [yachtUserWeb] => SimpleXMLElement Object
            (
            )

        [yachtVideo] => Array
            (
                [0] => SimpleXMLElement Object
                    (
                    )

                [1] => SimpleXMLElement Object
                    (
                    )

            )

        [yachtCruiseSpeed] => SimpleXMLElement Object
            (
            )

        [yachtMaxSpeed] => SimpleXMLElement Object
            (
            )

        [yachtAccommodations] => SimpleXMLElement Object
            (
            )

        [yachtHighPrice] => SimpleXMLElement Object
            (
            )

        [yachtLowPrice] => SimpleXMLElement Object
            (
            )

        [yachtPriceDetails] => Array
            (
                [0] => SimpleXMLElement Object
                    (
                    )

                [1] => SimpleXMLElement Object
                    (
                    )

            )

        [yachtTermsType] => SimpleXMLElement Object
            (
            )

        [yachtVcrDvd] => Yes
        [yachtSalonStereo] => Yes
        [yachtNumDvd] => SimpleXMLElement Object
            (
            )

        [yachtNumCds] => Yes
        [yachtBoardGames] => Yes
        [yachtNumBooks] => 20
        [yachtCamCorder] => No
        [yachtNumDineIn] => 8
        [yachtSunAwning] => Yes
        [yachtHammock] => No
        [yachtWindScoops] => No
        [yachtDeckShower] => Yes
        [yachtBimini] => Yes
        [yachtSpecialDiets] => Yes
        [yachtKosher] => No
        [yachtBBQ] => Yes
        [yachtGayCharters] => Yes
        [yachtNudeCharters] => No
        [yachtHairDryer] => Yes
        [yachtNumHatch] => SimpleXMLElement Object
            (
            )

        [yachtGuestSmoke] => Aft Deck
        [yachtGuestPet] => No
        [yachtChildrenAllowed] => Yes
        [yachtMinChildAge] => SimpleXMLElement Object
            (
            )

        [yachtGenerator] => 18 kw
        [yachtEngines] => SimpleXMLElement Object
            (
            )

        [yachtFuel] => SimpleXMLElement Object
            (
            )

        [yachtInverter] => Yes
        [yachtVoltages] => SimpleXMLElement Object
            (
            )

        [yachtWaterMaker] => Yes
        [yachtWaterCapacity] => 75 gallons
        [yachtIceMaker] => Yes
        [yachtDinghy] => SimpleXMLElement Object
            (
            )

        [yachtDinghyHp] => 70
        [yachtDinghyPax] => 10
        [yachtAdultWSkis] => No
        [yachtKidsSkis] => No
        [yachtJetSkis] => No
        [yachtWaveRun] => No
        [yachtKneeBoard] => Yes
        [yachtStandUpPaddle] => 0
        [yachtWindSurf] => No
        [yachtGearSnorkel] => 10
        [yachtTube] => Yes
        [yachtScurfer] => No
        [yachtWakeBoard] => Yes
        [yacht1ManKayak] => 2
        [yacht2ManKayak] => No
        [yachtFloatMats] => Yes
        [yachtSwimPlatform] => Yes
        [yachtBoardingLadder] => SimpleXMLElement Object
            (
            )

        [yachtDinghySailing] => No
        [yachtGamesBeach] => Yes
        [yachtFishingGear] => Yes
        [yachtFishGearType] => SimpleXMLElement Object
            (
            )

        [yachtNumFishRods] => SimpleXMLElement Object
            (
            )

        [yachtUnderWaterCam] => Yes
        [yachtUnderWaterVideo] => 0
        [yachtGreenMakeWater] => Yes
        [yachtGreenReuseBottle] => SimpleXMLElement Object
            (
            )

        [yachtGreenOther] => SimpleXMLElement Object
            (
            )

        [yachtScubaOnboard] => Onboard
        [yachtResortCourse] => SimpleXMLElement Object
            (
            )

        [yachtFullCourse] => SimpleXMLElement Object
            (
            )

        [yachtLicenseInfo] => SimpleXMLElement Object
            (
            )

        [yachtCompressor] => SimpleXMLElement Object
            (
            )

        [yachtNumDiveTanks] => 0
        [yachtNumBCS] => 0
        [yachtNumRegs] => 0
        [yachtNumWetSuits] => 0
        [yachtNumWeights] => 0
        [yachtNumDivers] => 0
        [yachtNumDives] => SimpleXMLElement Object
            (
            )

        [yachtNumNightDives] => 0
        [yachtNumDiveLights] => 1
        [yachtDiveInfo] => SimpleXMLElement Object
            (
            )

        [yachtDiveCosts] => SimpleXMLElement Object
            (
            )

        [yachtPic1] => SimpleXMLElement Object
            (
            )

        [yachtDesc1] => SimpleXMLElement Object
            (
            )

        [yachtPic2] => SimpleXMLElement Object
            (
            )

        [yachtDesc2] => SimpleXMLElement Object
            (
            )

        [yachtPic3] => SimpleXMLElement Object
            (
            )

        [yachtDesc3] => SimpleXMLElement Object
            (
            )

        [yachtPic4] => SimpleXMLElement Object
            (
            )

        [yachtDesc4] => SimpleXMLElement Object
            (
            )

        [yachtPic5] => SimpleXMLElement Object
            (
            )

        [yachtDesc5] => SimpleXMLElement Object
            (
            )

        [yachtPic6] => SimpleXMLElement Object
            (
            )

        [yachtDesc6] => SimpleXMLElement Object
            (
            )

        [yachtPic7] => SimpleXMLElement Object
            (
            )

        [yachtDesc7] => SimpleXMLElement Object
            (
            )

        [yachtPic8] => SimpleXMLElement Object
            (
            )

        [yachtDesc8] => SimpleXMLElement Object
            (
            )

        [yachtPic9] => SimpleXMLElement Object
            (
            )

        [yachtDesc9] => SimpleXMLElement Object
            (
            )

        [yachtPic10] => SimpleXMLElement Object
            (
            )

        [yachtDesc10] => SimpleXMLElement Object
            (
            )

        [yachtPic11] => SimpleXMLElement Object
            (
            )

        [yachtDesc11] => SimpleXMLElement Object
            (
            )

        [yachtPic12] => SimpleXMLElement Object
            (
            )

        [yachtDesc12] => SimpleXMLElement Object
            (
            )

        [yachtPic13] => SimpleXMLElement Object
            (
            )

        [yachtDesc13] => SimpleXMLElement Object
            (
            )

        [yachtPic14] => SimpleXMLElement Object
            (
            )

        [yachtDesc14] => SimpleXMLElement Object
            (
            )

        [yachtPic15] => SimpleXMLElement Object
            (
            )

        [yachtDesc15] => SimpleXMLElement Object
            (
            )

        [yachtPic16] => SimpleXMLElement Object
            (
            )

        [yachtDesc16] => SimpleXMLElement Object
            (
            )

        [yachtPic17] => SimpleXMLElement Object
            (
            )

        [yachtDesc17] => SimpleXMLElement Object
            (
            )

        [yachtPic18] => SimpleXMLElement Object
            (
            )

        [yachtDesc18] => SimpleXMLElement Object
            (
            )

        [yachtPic19] => SimpleXMLElement Object
            (
            )

        [yachtDesc19] => SimpleXMLElement Object
            (
            )

        [yachtLayout] => SimpleXMLElement Object
            (
            )

        [yachtFullEbrochure] => SimpleXMLElement Object
            (
            )

        [yachtFullRates] => SimpleXMLElement Object
            (
            )

        [yachtOtherToys] => SimpleXMLElement Object
            (
            )

        [yachtOtherEntertain] => SimpleXMLElement Object
            (
            )

        [yachtCommunicate] => SimpleXMLElement Object
            (
            )

        [yachtSummerArea] => SimpleXMLElement Object
            (
            )

        [yachtWinterArea] => SimpleXMLElement Object
            (
            )

        [yachtShowers] => SimpleXMLElement Object
            (
            )

        [yachtWashBasins] => SimpleXMLElement Object
            (
            )

        [yachtHeads] => SimpleXMLElement Object
            (
            )

        [yachtElectricHeads] => SimpleXMLElement Object
            (
            )

        [yachtPic2Large] => SimpleXMLElement Object
            (
            )

        [yachtPic3Large] => SimpleXMLElement Object
            (
            )

        [yachtPic4Large] => SimpleXMLElement Object
            (
            )

        [yachtPic5Large] => SimpleXMLElement Object
            (
            )

        [yachtPic6Large] => SimpleXMLElement Object
            (
            )

        [yachtPic7Large] => SimpleXMLElement Object
            (
            )

        [yachtPic8Large] => SimpleXMLElement Object
            (
            )

        [yachtPic9Large] => SimpleXMLElement Object
            (
            )

        [yachtPic10Large] => SimpleXMLElement Object
            (
            )

        [yachtPic11Large] => SimpleXMLElement Object
            (
            )

        [yachtPic12Large] => SimpleXMLElement Object
            (
            )

        [yachtPic13Large] => SimpleXMLElement Object
            (
            )

        [yachtPic14Large] => SimpleXMLElement Object
            (
            )

        [yachtPic15Large] => SimpleXMLElement Object
            (
            )

        [yachtPic16Large] => SimpleXMLElement Object
            (
            )

        [yachtPic17Large] => SimpleXMLElement Object
            (
            )

        [yachtPic18Large] => SimpleXMLElement Object
            (
            )

        [yachtPic19Large] => SimpleXMLElement Object
            (
            )

        [yachtSampleMenu] => SimpleXMLElement Object
            (
            )

        [yachtMenu1Pic] => SimpleXMLElement Object
            (
            )

        [yachtMenu2Pic] => SimpleXMLElement Object
            (
            )

        [yachtMenu3Pic] => SimpleXMLElement Object
            (
            )

        [yachtMenu4Pic] => SimpleXMLElement Object
            (
            )

        [yachtMenu5Pic] => SimpleXMLElement Object
            (
            )

        [yachtMenu6Pic] => SimpleXMLElement Object
            (
            )

        [yachtMenu7Pic] => SimpleXMLElement Object
            (
            )

        [yachtMenu8Pic] => SimpleXMLElement Object
            (
            )

        [yachtMenu9Pic] => SimpleXMLElement Object
            (
            )

        [yachtMenu10Pic] => SimpleXMLElement Object
            (
            )

        [yachtCrew] => 3
        [yachtCrewSmoke] => No
        [yachtCrewPets] => No
        [yachtCrewPetType] => SimpleXMLElement Object
            (
            )

        [yachtCaptainName] => SimpleXMLElement Object
            (
            )

        [yachtCaptainNation] => SimpleXMLElement Object
            (
            )

        [yachtCaptainBorn] => SimpleXMLElement Object
            (
            )

        [yachtCaptainLic] => SimpleXMLElement Object
            (
            )

        [yachtCaptainYrSail] => SimpleXMLElement Object
            (
            )

        [yachtCaptainYrChart] => SimpleXMLElement Object
            (
            )

        [yachtCaptainLang] => SimpleXMLElement Object
            (
            )

        [yachtCrewName] => SimpleXMLElement Object
            (
            )

        [yachtCrewNation] => SimpleXMLElement Object
            (
            )

        [yachtCrewLic] => SimpleXMLElement Object
            (
            )

        [yachtCrewYrSail] => SimpleXMLElement Object
            (
            )

        [yachtCrewYrChart] => SimpleXMLElement Object
            (
            )

        [yachtCrewLang] => SimpleXMLElement Object
            (
            )

        [yachtCrewProfile] => SimpleXMLElement Object
            (
            )

        [yachtCrewPhoto] => SimpleXMLElement Object
            (
            )

        [yachtCrew1Pic] => SimpleXMLElement Object
            (
            )

        [yachtCrew2Pic] => SimpleXMLElement Object
            (
            )

        [yachtCrew3Pic] => SimpleXMLElement Object
            (
            )

        [yachtCrew4Pic] => SimpleXMLElement Object
            (
            )

        [yachtCrew5Pic] => SimpleXMLElement Object
            (
            )

        [yachtCrew6Pic] => SimpleXMLElement Object
            (
            )

        [yachtCrew7Pic] => SimpleXMLElement Object
            (
            )

        [yachtCrew8Pic] => SimpleXMLElement Object
            (
            )

        [yachtCrew9Pic] => SimpleXMLElement Object
            (
            )

        [yachtCrew10Pic] => SimpleXMLElement Object
            (
            )

        [yachtCrew1Name] => SimpleXMLElement Object
            (
            )

        [yachtCrew2Name] => SimpleXMLElement Object
            (
            )

        [yachtCrew3Name] => SimpleXMLElement Object
            (
            )

        [yachtCrew4Name] => SimpleXMLElement Object
            (
            )

        [yachtCrew5Name] => SimpleXMLElement Object
            (
            )

        [yachtCrew6Name] => SimpleXMLElement Object
            (
            )

        [yachtCrew7Name] => SimpleXMLElement Object
            (
            )

        [yachtCrew8Name] => SimpleXMLElement Object
            (
            )

        [yachtCrew9Name] => SimpleXMLElement Object
            (
            )

        [yachtCrew10Name] => SimpleXMLElement Object
            (
            )

        [yachtCrew1Title] => SimpleXMLElement Object
            (
            )

        [yachtCrew2Title] => SimpleXMLElement Object
            (
            )

        [yachtCrew3Title] => SimpleXMLElement Object
            (
            )

        [yachtCrew4Title] => SimpleXMLElement Object
            (
            )

        [yachtCrew5Title] => SimpleXMLElement Object
            (
            )

        [yachtCrew6Title] => SimpleXMLElement Object
            (
            )

        [yachtCrew7Title] => SimpleXMLElement Object
            (
            )

        [yachtCrew8Title] => SimpleXMLElement Object
            (
            )

        [yachtCrew9Title] => SimpleXMLElement Object
            (
            )

        [yachtCrew10Title] => SimpleXMLElement Object
            (
            )

        [yachtFlag] => SimpleXMLElement Object
            (
            )

        [yachtHomePort] => SimpleXMLElement Object
            (
            )

        [yachtWBasePort] => SimpleXMLElement Object
            (
            )

        [yachtRig] => SimpleXMLElement Object
            (
            )

        [yachtGrossTons] => SimpleXMLElement Object
            (
            )

        [yachtAcNight] => SimpleXMLElement Object
            (
            )

        [yachtAcSurCharge] => SimpleXMLElement Object
            (
            )

        [yachtTubs] => SimpleXMLElement Object
            (
            )

        [yachtLocationDetails] => SimpleXMLElement Object
            (
            )

        [yachtTerms] => SimpleXMLElement Object
            (
            )

        [yachtCaptOnly] => SimpleXMLElement Object
            (
            )

        [yachtSpecialCon] => SimpleXMLElement Object
            (
            )

        [yachtContracts] => SimpleXMLElement Object
            (
            )

        [yachtConsumptionUnits] => SimpleXMLElement Object
            (
            )

        [yachtRange] => SimpleXMLElement Object
            (
            )

        [yachtPermit] => SimpleXMLElement Object
            (
            )

        [yachtLicense] => SimpleXMLElement Object
            (
            )

        [yachtMca] => SimpleXMLElement Object
            (
            )

        [yachtDeepSeaFish] => SimpleXMLElement Object
            (
            )

        [yachtSatTv] => SimpleXMLElement Object
            (
            )

        [yachtIpod] => SimpleXMLElement Object
            (
            )

        [yachtSailInstruct] => SimpleXMLElement Object
            (
            )

        [yachtInternet] => SimpleXMLElement Object
            (
            )

        [yachtBrokerNotes] => SimpleXMLElement Object
            (
            )

        [yachtInsCompany] => SimpleXMLElement Object
            (
            )

        [yachtPolicy] => SimpleXMLElement Object
            (
            )

        [yachtLiability] => SimpleXMLElement Object
            (
            )

        [yachtEffectiveDate] => SimpleXMLElement Object
            (
            )

        [yachtContractName] => SimpleXMLElement Object
            (
            )

        [yachtCoverageAreas] => SimpleXMLElement Object
            (
            )

        [yachtInsFlag] => SimpleXMLElement Object
            (
            )

        [yachtInsHomeport] => SimpleXMLElement Object
            (
            )

        [yachtRegNum] => SimpleXMLElement Object
            (
            )

        [yachtConPhone1] => SimpleXMLElement Object
            (
            )

        [yachtConPhone2] => SimpleXMLElement Object
            (
            )

        [yachtConPhone3] => SimpleXMLElement Object
            (
            )

        [yachtConFax] => SimpleXMLElement Object
            (
            )

        [yachtWaveConEmail] => SimpleXMLElement Object
            (
            )

        [yachtConOther] => SimpleXMLElement Object
            (
            )

        [yachtManager] => SimpleXMLElement Object
            (
            )

        [yachtManagerName] => SimpleXMLElement Object
            (
            )

        [yachtManagerPhone] => SimpleXMLElement Object
            (
            )

        [yachtManagerToll] => SimpleXMLElement Object
            (
            )

        [yachtManagerEmail] => SimpleXMLElement Object
            (
            )

      )

  )

3 个答案:

答案 0 :(得分:0)

假设描述将始终跟随图片,您可以使用modulo来确定当前正在循环的项目是图片还是相应的描述:

$i = 0; // Set up a counter
foreach ($xml_ebrochure as $value_ebrochure) {
    if ($value_ebrochure[$i] % 2 === 0) {
        // You've got a picture
        $name = 'yachtPic';
    } else {
        // You've got the description
    }
    $i++; // Increment the counter
}

答案 1 :(得分:0)

标签的名称已存在于第一个循环中,您只需要指定要使用它:

$xml_ebrochure = simplexml_load_file($url);
foreach ($xml_ebrochure as $key => $value_ebrochure) { // $key will contain the tag name
    $name = 'yachtPic';
      if (strpos($key, $name) !== false) {
        echo $key.": ".$value_ebrochure;
    }

}

请注意,检查$name是否存在的条件略有不同。您需要将strpos()false的结果与strict comparison进行比较,在这种情况下,因为字符串完全匹配后会返回0(匹配从第一个开始)位置),你进行了>比较。

Demo

答案 2 :(得分:0)

如果您只需要yactPic,则可能需要使用XPath来获取它们:

$xml = '<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<root>
    <yachtPic1>.p1.</yachtPic1>
    <yachtDesc1>.d1.</yachtDesc1>
    <yachtPic2>.p2.</yachtPic2>
    <yachtDesc2>.d2.</yachtDesc2>
    <yachtPic3>.p3.</yachtPic3>
    <yachtDesc3>.v3.</yachtDesc3>
</root>';

$root = new SimpleXMLElement($xml);

$res = $root->xpath('*[starts-with(local-name(), "yachtPic")]');

$pics = [];

foreach ($res as $elem) {
    $pics[] = $elem->__toString();
}

var_dump($pics);

将输出:

array(3) {
  [0] =>
  string(4) ".p1."
  [1] =>
  string(4) ".p2."
  [2] =>
  string(4) ".p3."
}

如果您需要合并图片和说明:

$xml = '...';

$root = new SimpleXMLElement($xml);

$pics = [];
foreach ($root->xpath('*[starts-with(local-name(), "yachtPic")]') as $elem) {
    $picDescTagName = preg_replace('/Pic(\d+)$/', 'Desc$1', $elem->getName());
    $pics[$picDescTagName]['pic'] = $elem->__toString();
}
unset($elem, $picDescTagName);

foreach ($root->xpath('*[starts-with(local-name(), "yachtDesc")]') as $elem) {
    $picDescTagName = $elem->getName();
    $pics[$picDescTagName]['desc'] = $elem->__toString();
}
unset($elem, $picDescTagName);

$pics = array_values($pics);
print_r($pics);

输出是:

Array
(
    [0] => Array
        (
            [pic] => .p1.
            [desc] => .d1.
        )

    [1] => Array
        (
            [pic] => .p2.
            [desc] => .d2.
        )

    [2] => Array
        (
            [pic] => .p3.
            [desc] => .v3.
        )

)