SimpleXMLElement上的foreach循环不起作用,结果只有1个元素

时间:2017-04-05 10:56:04

标签: php arrays simplexml

我有这种结构的xml文件:

$xmlString = '
  <plist>
    <categories>
      <category>
        <id>1</id>
        <parent>0</parent>
        <description><![CDATA[Test 1]]></description>
      </category>
      <category>
        <id>2</id>
        <parent>1</parent>
        <description><![CDATA[Test 1.1]]></description>
      </category>
      <category>
        <id>3</id>
        <parent>1</parent>
        <description><![CDATA[Test 1.2]]></description>
      </category>
      </categories>
  </plist>';

现在我正在尝试以这种方式构建一个数组:

$xmlData = new SimpleXMLElement($xmlString);

$results = [];

foreach($xmlData->categories->category as $key => $category){
  $results[$key]['id']       = isset($category->id) ? (string)$category->id : false;
  $results[$key]['parentId'] = isset($category->parent) ? (string)$category->parent : false;
  $results[$key]['name']     = isset($category->description) ? (string)$category->description : false;
}

echo '<pre>'.print_r($results,true).'</pre>';

但结果只是最后一项:

Array
(
  [category] => Array
    (
      [id] => 3
      [parentId] => 1
      [name] => Test 1.2
    )

)

SimpleXMLElement对象如下所示:

SimpleXMLElement Object
(
  [categories] => SimpleXMLElement Object
    (
      [category] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [id] => 1
                    [parent] => 0
                    [description] => SimpleXMLElement Object
                        (
                        )

                )

            [1] => SimpleXMLElement Object
                (
                    [id] => 2
                    [parent] => 1
                    [description] => SimpleXMLElement Object
                        (
                        )

                )

            [2] => SimpleXMLElement Object
                (
                    [id] => 3
                    [parent] => 1
                    [description] => SimpleXMLElement Object
                        (
                        )

                  )

            )

      )

)

所以类别是一个数组,我不明白为什么foreach循环不起作用。关键应该是1,2,3还是不?

4 个答案:

答案 0 :(得分:8)

用这个替换你的循环。 $key始终是一个单一的密钥。所以它每次都在覆盖

$counter=0;

foreach($xmlData->categories->category as $category){
  $results[$counter]['id']       = isset($category->id) ? (string)$category->id : false;
  $results[$counter]['parentId'] = isset($category->parent) ? (string)$category->parent : false;
  $results[$counter]['name']     = isset($category->description) ? (string)$category->description : false;
  $counter++;
}

答案 1 :(得分:1)

好的,这里有一个没有计数器但只有简单[]的答案:

foreach($xmlData->categories->category as $category){
    $results[] = array(
        'id' => isset($category->id) ? (string)$category->id : false,
        'parentId' => isset($category->parent) ? (string)$category->parent : false,
        'name' => isset($category->description) ? (string)$category->description : false,
    );
}

答案 2 :(得分:0)

像这样使用

 $('.mesureinitiale').each(function(){

 })

答案 3 :(得分:-1)

使用此:

$xmlString = '
  <plist>
    <categories>
      <category>
        <id>1</id>
        <parent>0</parent>
        <description><![CDATA[Test 1]]></description>
      </category>
      <category>
        <id>2</id>
        <parent>1</parent>
        <description><![CDATA[Test 1.1]]></description>
      </category>
      <category>
        <id>3</id>
        <parent>1</parent>
        <description><![CDATA[Test 1.2]]></description>
      </category>
      </categories>
  </plist>';

$xml = simplexml_load_string($xmlString, "SimpleXMLElement", LIBXML_NOCDATA | LIBXML_NOWARNING | LIBXML_NOERROR | LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);

$json = json_encode($xml);
$data = json_decode($json, true);
// print_r($data);
$result = array();

if(isset($data['categories']['category'])) {
    foreach($data['categories']['category'] as $array){
        // print_r($array);
        $result[] = $array;
    }
}
print_r($result);

结果:

Array
(
    [0] => Array
        (
            [id] => 1
            [parent] => 0
            [description] => Test 1
        )

    [1] => Array
        (
            [id] => 2
            [parent] => 1
            [description] => Test 1.1
        )

    [2] => Array
        (
            [id] => 3
            [parent] => 1
            [description] => Test 1.2
        )

)