简体   繁体   中英

foreach loop over SimpleXMLElement not working, only 1 element as result

I have xml file with this structure:

$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>';

Now I'm trying to build an array this way:

$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>';

But the result is only the last entry:

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

)

The SimpleXMLElement Object looks like this:

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
                        (
                        )

                  )

            )

      )

)

So category is an array and i don't understand why the foreach loop isn't working. The key Should be 1,2,3 or not?

Replace you loop with this. $key was always a single and same key. so it was over-writting everytime

$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++;
}

Okay, here's an answer without counters but with simple [] :

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,
    );
}

use like this

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

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

Use this:

$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);

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
        )

)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM