Don't like ads? PRO users don't see any ads ;-)

json_encode child element attributes

By: JPeterson on Apr 14th, 2012  |  syntax: PHP  |  size: 0.17 KB  |  hits: 37  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. header("Content-Type: text/plain");
  3. $xml = <<<XML
  4. <a>
  5.   <b a="value">text</b>
  6. </a>
  7. XML;
  8. print_r(json_encode((array)simplexml_load_string($xml)));
  9. // {"b":"text"}