How to get second layer output from XML and convert to php variable ?

Hi,

I have the following scripts :


<?

$xml="<whmcsapi>
<action>getinvoice</action>
<result>success</result>
<invoiceid>242347</invoiceid>
<invoicenum></invoicenum>
<userid>25209</userid>
<date>2017-12-21</date>
<duedate>2018-01-11</duedate>
<datepaid>0000-00-00 00:00:00</datepaid>
<lastcaptureattempt>0000-00-00 00:00:00</lastcaptureattempt>
<subtotal>50.00</subtotal>
<credit>0.00</credit>
<tax>3.00</tax>
<tax2>0.00</tax2>
<total>53.00</total>
<balance>53.00</balance>
<taxrate>6.00</taxrate>
<taxrate2>0.00</taxrate2>
<status>Unpaid</status>
<paymentmethod>banktransfer</paymentmethod>
<notes></notes>
<ccgateway></ccgateway>
     <items>
                   <item>
                                <id>304494</id>
                                <type>Domain</type>
                                <relid>52025</relid>
                                <description>Domain Renewal - xxx.com - 1 Year/s (11/01/2018 - 10/01/2019)</description>
                                <amount>50.00</amount>
                                <taxed>1</taxed>
                  </item>
     </items>
<transactions></transactions>
</whmcsapi>";


$content .= '<table border="1" cellpadding="5" style="border-collapse: collapse;border: 1px solid #cacaca">';

$content .= '<tr><th colspan="2" bgcolor="thistle">ITEMS '.print_r($xml).'</th></tr>';
foreach ($xml['items']['item'] as $key => $value) {

  $content .= '<tr><td><b>Description</b></td><td>'.$value['description'].'</td></tr>';
  $content .= '<tr><td><b>Amount</b></td><td>'.$value['amount'].'</td></tr>';
}
	

	$content .= '</table>';

return $content;
?>

Open in new window



But i received the following error :

Warning: Illegal string offset 'items' in /home/internet/public_html/test/teststring4.php on line 42

Warning: Illegal string offset 'item' in /home/internet/public_html/test/teststring4.php on line 42

Warning: Invalid argument supplied for foreach() in /home/internet/public_html/test/teststring4.php on line 42


May i know how to get the variable inside the "items" and "item" ?

I have tried "foreach ($xml['items']['item'] as $key => $value)" but it produce the above error.

Hope anybody can help me by providing the correct script.
LVL 2
Shamsul KamalJunior TechAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gr8gonzoConsultantCommented:
Right now, $xml is just a string that contains raw, unparsed XML. You can't loop through it any more than you could loop through the phrase "Hello world!". You can use the SimpleXML extension to parse the XML string into an object that you can then loop through.

Here's the documentation page on how to do it:
http://php.net/manual/en/simplexml.examples-basic.php
0
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
try:

<?php

$xml="<whmcsapi>
  <action>getinvoice</action>
  <result>success</result>
  <invoiceid>242347</invoiceid>
  <invoicenum></invoicenum>
  <userid>25209</userid>
  <date>2017-12-21</date>
  <duedate>2018-01-11</duedate>
  <datepaid>0000-00-00 00:00:00</datepaid>
  <lastcaptureattempt>0000-00-00 00:00:00</lastcaptureattempt>
  <subtotal>50.00</subtotal>
  <credit>0.00</credit>
  <tax>3.00</tax>
  <tax2>0.00</tax2>
  <total>53.00</total>
  <balance>53.00</balance>
  <taxrate>6.00</taxrate>
  <taxrate2>0.00</taxrate2>
  <status>Unpaid</status>
  <paymentmethod>banktransfer</paymentmethod>
  <notes></notes>
  <ccgateway></ccgateway>
  <items>
    <item>
      <id>304494</id>
      <type>Domain</type>
      <relid>52025</relid>
      <description>Domain Renewal - xxx.com - 1 Year/s (11/01/2018 - 10/01/2019)</description>
      <amount>50.00</amount>
      <taxed>1</taxed>
    </item>
  </items>
  <transactions></transactions>
</whmcsapi>";

$content = '';

$doc = new SimpleXMLElement($xml);

$content .= '<table border="1" cellpadding="5" style="border-collapse: collapse;border: 1px solid #cacaca">';

  //$content .= '<tr> <th colspan="2" bgcolor="thistle">ITEMS '.print_r($xml).'</th></tr>';
  $content .= '<tr> <th colspan="2" bgcolor="thistle">ITEMS</th></tr>';
  
  foreach ($doc->items->item as $node) {
    $content .= '<tr><td><b>Description</b></td><td>'.$node->description.'</td></tr>';
    $content .= '<tr><td><b>Amount</b></td><td>'.$node->amount.'</td></tr>';
  }
  
  $content .= '</table>';

  echo $content;
  //return $content;

?>

Open in new window


or:

<?php

$xml="<whmcsapi>
  <action>getinvoice</action>
  <result>success</result>
  <invoiceid>242347</invoiceid>
  <invoicenum></invoicenum>
  <userid>25209</userid>
  <date>2017-12-21</date>
  <duedate>2018-01-11</duedate>
  <datepaid>0000-00-00 00:00:00</datepaid>
  <lastcaptureattempt>0000-00-00 00:00:00</lastcaptureattempt>
  <subtotal>50.00</subtotal>
  <credit>0.00</credit>
  <tax>3.00</tax>
  <tax2>0.00</tax2>
  <total>53.00</total>
  <balance>53.00</balance>
  <taxrate>6.00</taxrate>
  <taxrate2>0.00</taxrate2>
  <status>Unpaid</status>
  <paymentmethod>banktransfer</paymentmethod>
  <notes></notes>
  <ccgateway></ccgateway>
  <items>
    <item>
      <id>304494</id>
      <type>Domain</type>
      <relid>52025</relid>
      <description>Domain Renewal - xxx.com - 1 Year/s (11/01/2018 - 10/01/2019)</description>
      <amount>50.00</amount>
      <taxed>1</taxed>
    </item>
  </items>
  <transactions></transactions>
</whmcsapi>";

$content = '';


$doc = new DOMDocument();
$doc->loadXML($xml);

$content .= '<table border="1" cellpadding="5" style="border-collapse: collapse;border: 1px solid #cacaca">';

  //$content .= '<tr> <th colspan="2" bgcolor="thistle">ITEMS '.print_r($xml).'</th></tr>';
  $content .= '<tr> <th colspan="2" bgcolor="thistle">ITEMS</th></tr>';
  
  foreach ($doc->getElementsByTagName('items') as $node) {
    $itms = preg_split("/\\r\\n|\\r|\\n/", $node->nodeValue);
    $content .= '<tr><td><b>Description</b></td><td>'.$itms[5].'</td></tr>';
    $content .= '<tr><td><b>Amount</b></td><td>'.$itms[6].'</td></tr>';
  }
  
  $content .= '</table>';

  echo $content;
  //return $content;

?>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Shamsul KamalJunior TechAuthor Commented:
Thank you.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.