1

I have an xml file with the following structure:

<categories>
  <category>
    <id>3</id>
    <title><![CDATA[Testname]]></title>
    <text><![CDATA[]]></text>
    <keywords><![CDATA[]]></keywords>
    <description><![CDATA[]]></description>
  </category>
</categories>

Now I'm loading this file and creating an array of it:

$xmlData = simplexml_load_file( 'categories.xml', null, LIBXML_NOCDATA);
$array = json_decode(json_encode($xmlData), true);

That generates me the following Result (print_r Output):

Array
(
  [@attributes] => Array
    (
      [version] => 1.0
    )

  [categories] => Array
    (
      [category] => Array
        (
          [0] => Array
            (
              [id] => 3
              [title] => Testname
              [text] => Array
                (
                )

              [keywords] => Array
                (
                )

              [description] => Array
                (
                )
            )
        )
    )
)

Here is my question, how could I remove those empty arrays? I tried it with array Filter, but this didn't work. (I need the keys, but they should be empty)

I know there would be a way, in my next step where I'm renaming the array keys as needed, i could check for empty arrays in the foreach loop, but I think there is an easier way, because every field (except id) could be empty in the xml file.

foreach($array['categories']['category'] as $key => $category){
  $results[$key]['id'] = $category['id'];
  $results[$key]['headline'] = $category['title'];
  $results[$key]['content'] = $category['text'];
  $results[$key]['metaKeywords'] = $category['keywords'];
  $results[$key]['metaDescription'] = $category['description'];
}

Does someone has an idea, what i could do after the json_decode? Or is there an easier way for all I'm trying to accomplish here?

Thanks!

1
  • So you first iterate over categories to remove empty values, and then again iterate over categories to create $results. Why do you need to do it tiwce? Commented Mar 30, 2017 at 8:41

2 Answers 2

2

Every time I see someone use that json_decode(json_encode()) hack, it makes me sad. You don't need to turn the SimpleXML object into an array to loop over it, just read the usage examples in the manual.

If you loop over the SimpleXML object directly, you will never get those arrays, so will never need to remove them:

$xmlData = simplexml_load_file('categories.xml'); // LIBXML_NOCDATA is NOT needed
foreach($xmlData->categories->category as $key => $category){
  $results[$key]['id'] = (string)$category->id;
  $results[$key]['headline'] = (string)$category->title;
  $results[$key]['content'] = (string)$category->text;
  $results[$key]['metaKeywords'] = (string)$category->keywords;
  $results[$key]['metaDescription'] = (string)$category->description;
}

The (string) tells SimpleXML you want the text content of a particular element (including CDATA), and will give you an empty string for the empty elements.

Sign up to request clarification or add additional context in comments.

1 Comment

I've tried it like you suggested, but I'm not getting a full array. Please take a look here: stackoverflow.com/questions/43229159/…
0

Hope this will work. PHP code demo

<?php
$result=Array
(

  "categories" => Array
    (
      "category" => Array
        (
          0 => Array
            (
              "id" => 3,
              "title" => "Testname",
              "text" => Array
                (
                ),

              "keywords" => Array
                (
                ),

              "description" => Array
                (
                )
            )
        )
    )
);
$results=array();
foreach($result['categories']['category'] as $key => $category){
  $results[$key]['id'] = $category['id'];
  $results[$key]['headline'] = $category['title'];
  $results[$key]['content'] = is_array($category['text']) && count($category['text'])>0 ? $category['text'] : false;
  $results[$key]['metaKeywords'] = is_array($category['keywords']) && count($category['keywords'])>0 ? $category['keywords'] : false;
  $results[$key]['metaDescription'] = is_array($category['description']) && count($category['description'])>0 ? $category['description'] : false;
  $results[$key]=array_filter($results[$key]);
}
print_r($results);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.