2

I have an array list of tags from xml file and im using xml file to add some posts to wordpress. So i need to convert xml array to comma separated list for tags to import in my posts.

This is an xml file example

<tags>
   <tag>tag1</tag>
   <tag>tag2</tag>
   <tag>tag3</tag>
</tags>

So when i call this file in my .php file and use print_r to get the output i get this

SimpleXMLElement Object ( 
    [tag] => Array ( 
         [0] => SimpleXMLElement Object ( )
         [1] => SimpleXMLElement Object ( )
         [2] => SimpleXMLElement Object ( )
         [3] => SimpleXMLElement Object ( )
    )
)

So 0 1 2 3 are tags and values are stored in SimpleXMLElement Object ( ) i read it's normal to not get values when using print_r but i know the values are there.

Now i need to convert this as a list at the end to get this result

 $post_tags = tag1, tag2, tag3;

so that i can use $post_tags in my function.

3
  • Your sample XML has only 3 <tag> nodes, but your SimpleXML object has 4. Can you please post the code you used to get the SimplXMLElement you showed above via print_r()? Commented Feb 16, 2014 at 21:48
  • Is this really important, the xml files will be imported from various different sources so in real world none of imported files will have same number of tags, and number of tags is not related to the function that i need. Commented Feb 16, 2014 at 22:04
  • No, I just want to verify what you are asking. Commented Feb 16, 2014 at 22:08

1 Answer 1

1

Assuming your object (the one on which you called print_r() is $xml), you would loop over $xml->tag and append each child object's contents to an array. Finally, implode the array to a string.

// An array to hold it temporarily
$post_tags_arr = array();

foreach ($xml->tag as $t) {
  // Cast it to a string to get the text value back
  $post_tags_arr[] = (string)$t;
}
// Implode the array to a string
$post_tags = implode(', ', $post_tags_arr);
echo $post_tags;

You indicate that you need a list for $post_tags, but your syntax above is ambiguous as to whether you wanted a string or an array. If you wanted an array, you have it in $post_tags_arr prior to the implode().

If you feel like being clever, and the <tag> nodes reside at the same level and have no children, you can simply cast them as a regular array, which will result in their string values.

// Cast it to an array in one go:
$post_tags_arr = (array)$xml->tag;
print_r($post_tags_arr);

Array
(
    [0] => tag1
    [1] => tag2
    [2] => tag3
)
Sign up to request clarification or add additional context in comments.

3 Comments

I have tried with really similar function as you posted above but i was getting an error like implode(): Invalid arguments passed ill check out again and compare with yours to see what am i doing wrong. As to your question the end result should be tags lined up and comma separated so i can use it in WordPress wp_insert_post. this is tags attribute to wp_insert_post what i need 'tags_input' => 'tag1, tag2, tag3'
If this doesn't work exactly as I have written it (except for the $xml variable name), please post the code above where you instantiate SimpleXML to parse your input XML.
It works, i was missing stupid brackets, so i was using like $post_tags_arr instead $post_tags_arr[] inside foreach loop. Thanks you helped me see what i was doing wrong.

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.