SimpleXML来自另一个数组的多个数据

时间:2014-05-22 08:36:04

标签: php arrays xml parsing simplexml

拜托,我有这个代码将数据从XML导入数据库:

$a = glob('data/*/*.xml');

echo "import kategorie ...... ";
foreach ($a as $i) {
    $xml = simplexml_load_file("$i") or die ("Chyba: Nemuzu nacist soubor");

    foreach($xml->DocumentElement as $entry) {
        foreach ($entry->hotel as $dataHotel) {
            addCategory("$dataHotel->country", "$dataHotel->location", "$dataHotel->location2");
        }
        foreach ($entry->Popisy as $dataPopisy) {
            addHotel("$dataHotel->hotel", "$dataPopisy->doporuc");
        }
    }
}
echo "OK\n"

我无法弄明白该怎么做 - 我需要在函数“addHotel”中从“hotel”数组和“Popisy”数组中获取数据。 所以,我需要立刻从两个人那里获得。

这是XML结构:http://pastebin.com/TNTpBijghttp://fmnet.cz/HLS240.xml

这可能吗?非常感谢你!


现在我尝试了这个:

$a = glob('data/*/*.xml');

echo "import kategorie ...... ";
foreach ($a as $i) {
    $xml = simplexml_load_file("$i") or die ("Chyba: Nemuzu nacist soubor");

    foreach($xml->DocumentElement as $entry) {
        foreach ($entry->hotel as $dataHotel) {
            //addCategory("$dataHotel->country", "$dataHotel->location", "$dataHotel->location2");
            foreach ($entry->Popisy as $dataPopisy) {
               //addHotel("$dataHotel->hotel", "$dataPopisy->doporuc");
                echo "$dataHotel->hotel";
                echo "\n";
                echo "$dataPopisy->doporuc";
                echo "\n";
            }
        }
    }
}
echo "OK\n";

但仅输出: import kategorie ......好的

1 个答案:

答案 0 :(得分:1)

是的,有可能。您可以通过将popisy循环嵌入酒店循环或其他方式来实现。检查以下代码:

$a = glob('data/*/*.xml');

echo "import kategorie ...... ";
foreach ($a as $i) {
    $xml = simplexml_load_file("$i") or die ("Chyba: Nemuzu nacist soubor");

    foreach($xml->DocumentElement as $entry) {
        foreach ($entry->hotel as $dataHotel) {
            addCategory("$dataHotel->country", "$dataHotel->location", "$dataHotel->location2");
            foreach ($entry->Popisy as $dataPopisy) {
               addHotel("$dataHotel->hotel", "$dataPopisy->doporuc");
            }
        }
    }
}
echo "OK\n"