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!
categoriesto remove empty values, and then again iterate overcategoriesto create$results. Why do you need to do it tiwce?