SimpleXML:foreach - 缺少一些元素

时间:2015-01-23 19:56:36

标签: php xml foreach

我有以下XML:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE SHOP PUBLIC "XSLT-compat" "">
<SHOP>
  <SHOPITEM>

    <FRAMESIZE>16"</FRAMESIZE>
    <CODE>032,74</CODE>
    <PRODUCT>TARKA  3.0</PRODUCT>
    <COLOR>black / blue</COLOR>
    <PRICE>499.99</PRICE>
    <PRODUCT_VARIANT id="2">
      <FRAMESIZE>18"</FRAMESIZE>
      <CODE>032,75</CODE>
      <PRODUCT>TARKA  3.0</PRODUCT>
      <COLOR>black / blue</COLOR>
      <PRICE>499.99</PRICE>
    </PRODUCT_VARIANT>
    <PRODUCT_VARIANT id="3">
      <FRAMESIZE>20"</FRAMESIZE>
      <CODE>032,76</CODE>
      <PRODUCT>TARKA  3.0</PRODUCT>
      <COLOR>black / blue</COLOR>
      <PRICE>499.99</PRICE>
    </PRODUCT_VARIANT>
  </SHOPITEM>
  <SHOPITEM>
    <FRAMESIZE>16"</FRAMESIZE>
    <CODE>032,77</CODE>
    <PRODUCT>TARKA  2.0</PRODUCT>
    <COLOR>black / green</COLOR>
    <PRICE>399.99</PRICE>
    <PRODUCT_VARIANT id="2">
      <FRAMESIZE>18"</FRAMESIZE>
      <CODE>032,78</CODE>
      <PRODUCT>TARKA  2.0</PRODUCT>
      <COLOR>black / green</COLOR>
      <PRICE>399.99</PRICE>
    </PRODUCT_VARIANT>
    <PRODUCT_VARIANT id="3">
      <FRAMESIZE>20"</FRAMESIZE>
      <CODE>032,79</CODE>
      <PRODUCT>TARKA  2.0</PRODUCT>
      <COLOR>black / green</COLOR>
      <PRICE>399.99</PRICE>
    </PRODUCT_VARIANT>
  </SHOPITEM>
  <SHOPITEM>
    <FRAMESIZE>16"</FRAMESIZE>
    <CODE>032,80</CODE>
    <PRODUCT>TARKA  1.0</PRODUCT>
    <COLOR>black / red</COLOR>
    <PRICE>349.99</PRICE>
    <PRODUCT_VARIANT id="2">
      <FRAMESIZE>18"</FRAMESIZE>
      <CODE>032,81</CODE>
      <PRODUCT>TARKA  1.0</PRODUCT>
      <COLOR>black / red</COLOR>
      <PRICE>349.99</PRICE>
    </PRODUCT_VARIANT>
    <PRODUCT_VARIANT id="3">
      <FRAMESIZE>20"</FRAMESIZE>
      <CODE>032,82</CODE>
      <PRODUCT>TARKA  1.0</PRODUCT>
      <COLOR>black / red</COLOR>
      <PRICE>349.99</PRICE>
    </PRODUCT_VARIANT>
  </SHOPITEM>
  <SHOPITEM>
    <FRAMESIZE>17"</FRAMESIZE>
    <CODE>032,83</CODE>
    <PRODUCT>TERRA 3.0</PRODUCT>
    <COLOR>matte black / red</COLOR>
    <PRICE>399.99</PRICE>
    <PRODUCT_VARIANT id="2">
      <FRAMESIZE>17"</FRAMESIZE>
      <CODE>032,84</CODE>
      <PRODUCT>TERRA 3.0</PRODUCT>
      <COLOR>black / green</COLOR>
      <PRICE>399.99</PRICE>
    </PRODUCT_VARIANT>
    <PRODUCT_VARIANT id="3">
      <FRAMESIZE>19"</FRAMESIZE>
      <CODE>032,85</CODE>
      <PRODUCT>TERRA 3.0</PRODUCT>
      <COLOR>matte black / red</COLOR>
      <PRICE>399.99</PRICE>
    </PRODUCT_VARIANT>
    <PRODUCT_VARIANT id="4">
      <FRAMESIZE>19"</FRAMESIZE>
      <CODE>032,86</CODE>
      <PRODUCT>TERRA 3.0</PRODUCT>
      <COLOR>black / green</COLOR>
      <PRICE>399.99</PRICE>
    </PRODUCT_VARIANT>
    <PRODUCT_VARIANT id="5">
      <FRAMESIZE>21"</FRAMESIZE>
      <CODE>032,87</CODE>
      <PRODUCT>TERRA 3.0</PRODUCT>
      <COLOR>matte black / red</COLOR>
      <PRICE>399.99</PRICE>
    </PRODUCT_VARIANT>
    <PRODUCT_VARIANT id="6">
      <FRAMESIZE>21"</FRAMESIZE>
      <CODE>032,88</CODE>
      <PRODUCT>TERRA 3.0</PRODUCT>
      <COLOR>black / green</COLOR>
      <PRICE>399.99</PRICE>
    </PRODUCT_VARIANT>
  </SHOPITEM>
</SHOP>

这是我目前循环使用XML的PHP​​代码:

foreach($xml->SHOPITEM as $izdelki)
{   
$artikel = (array)$izdelki;

$izdelek = array();
echo $artikel['PRODUCT'] . '-' . $artikel['COLOR'] . '-' . $artikel['FRAMESIZE'] . '<br>';

    //get product_variant elements
    foreach($xml->SHOPITEM->PRODUCT_VARIANT as $p_variant)
        {
            $artikel2 = (array)$p_variant;
            echo $artikel2['PRODUCT'] . '-' . $artikel2['COLOR'] . '-' . $artikel2['FRAMESIZE'] . " main code: " . $artikel['CODE'] . '<br>';
        }

} 

输出:

TARKA 3.0-black / blue-16"
TARKA 3.0-black / blue-18" main code: 032,74
TARKA 3.0-black / blue-20" main code: 032,74
TARKA 2.0-black / green-16"
TARKA 3.0-black / blue-18" main code: 032,77
TARKA 3.0-black / blue-20" main code: 032,77
TARKA 1.0-black / red-16"
TARKA 3.0-black / blue-18" main code: 032,80
TARKA 3.0-black / blue-20" main code: 032,80
TERRA 3.0-matte black / red-17"
TARKA 3.0-black / blue-18" main code: 032,83
TARKA 3.0-black / blue-20" main code: 032,83

问题:输出绝对错误,foreach功能不是从产品到产品。你能指点一下这段代码在哪里破碎吗?

1 个答案:

答案 0 :(得分:1)

看起来您的第二个循环不会停留在当前SHOPITEM内。换句话说

foreach($xml->SHOPITEM->PRODUCT_VARIANT as $p_variant)

应该是

foreach($izdelki->PRODUCT_VARIANT as $p_variant)