Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8121925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:39:45+00:00 2026-06-06T05:39:45+00:00

I’ve been researching this problem, but I can’t find what I need. It shouldn’t

  • 0

I’ve been researching this problem, but I can’t find what I need. It shouldn’t be difficult, probably it’s a matter of syntax =)

I create a string representing an XML inside the function like this:

$sxe = simplexml_load_string('
<xmlFile>
 <item param="'.$variable.'">
  <subitem>'.$var2s.'</subitem>
 </item>
</xmlFile>
');

The contents of the variables are plain strings like this abc,def,ghi in both variables which I obtain from a hash this way:

isset($variable);
$variable="";
isset($vars2);
$vars2="";

foreach ($hashArray as $stringKey => $stringValue) {
 // I separate each result with a comma
 $variable .= $stringKey.",";
 $vars2 .= $stringValue.",";
}
// Then remove the last comma
$variable = substr($variable, 0, -1);
$vars2 = substr($vars2, 0, -1);

When I save my XML with $sxe->asXml('xml/myGreatFile.xml'); I got something similar to:

<xmlFile>
 <item param="abc,def,ghi">
  <subitem>JKL,MNO,PQR</subitem>
 </item>
</xmlFile>

That was fine but now for my new requirement I need a result similar to this:

<xmlFile>
 <item param="abc">
  <subitem>JKL</subitem>
 </item>
 <item param="def">
  <subitem>MNO</subitem>
 </item>
 <item param="ghi">
  <subitem>PQR</subitem>
 </item>
</xmlFile>

How can I create this repetitive node? I tried to concatenate PHP functions inside the simplexml_load_string string as I did with the variables but seems to be it’s not possible:

$sxe = simplexml_load_string('
<xmlFile>'.
 // Syntax Error u_u
 foreach ($hashArray as $stringKey => $stringValue) {
  $variable .= $stringKey.",";
  $vars2 .= $stringValue.",";.
 
 '<item param="'.$variable.'">
  <subitem>'.$var2s.'</subitem>
 </item>'.
 }
.'</xmlFile>
');

Of course my syntax it’s wrong, but I want to create this repetitive node somehow, maybe with a cycle and maybe using my hash array directly instead of passing it to string.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T05:39:47+00:00Added an answer on June 6, 2026 at 5:39 am

    The answer is pretty simple: build your string variable outside of the simplexml function and then use it in the function.

      $mystring = "<xmlFile>";
      foreach($array as $key => $value)
      {
          $mystring .= "<item param='$key'><subitem>$value</subitem></item>";
      }
      $mystring .= "</xmlFile>";
    
      $sxe = simplexml_load_string($mystring);
    

    And if you’re using a multidimensional array? Just nest your foreach string building statements.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.