@ 2015-03-10T19:06:18Z <?php
/**
* @link http://stackoverflow.com/questions/28965063/php-access-array-within-xml
*/
$buffer = <<<XML
<root>
<merchant id="1" name="companyname">
<prod id="690579815" lang="en" pre_order="no" web_offer="yes" in_stock="yes">product #1</prod>
<prod id="250544605" lang="en" pre_order="no" web_offer="yes" in_stock="yes">product #2</prod>
<prod id="360355798" lang="en" pre_order="no" web_offer="yes" in_stock="yes">product #3</prod>
</merchant>
</root>
XML;
$data = simplexml_load_string($buffer);
var_dump($data['prod']);
var_dump(count($data['prod']));
$merchant = $data->merchant;
var_dump(count($merchant->prod));
var_dump($merchant->prod[0]->asXML());
var_dump($merchant->prod[0]['id']->asXML());
var_dump((string)$merchant->prod[0]);
var_dump((string)$merchant->prod[0]['id']);
echo $merchant->prod[0]; // prints: "product #1"
echo "\n";
foreach ($merchant->prod as $prod)
{
echo $prod->asXML(), "\n";
}
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 8.0.10 - 8.0.30 , 8.1.0 - 8.1.28 , 8.2.0 - 8.2.18 , 8.3.0 - 8.3.6 NULL
Fatal error: Uncaught TypeError: count(): Argument #1 ($value) must be of type Countable|array, null given in /in/HuLc7:19
Stack trace:
#0 {main}
thrown in /in/HuLc7 on line 19
Process exited with code 255 . Output for 8.0.0 - 8.0.9 NULL
Fatal error: Uncaught TypeError: count(): Argument #1 ($var) must be of type Countable|array, null given in /in/HuLc7:19
Stack trace:
#0 {main}
thrown in /in/HuLc7 on line 19
Process exited with code 255 . Output for 7.2.0 - 7.2.33 , 7.3.0 - 7.3.33 , 7.4.0 - 7.4.33 NULL
Warning: count(): Parameter must be an array or an object that implements Countable in /in/HuLc7 on line 19
int(0)
int(3)
string(94) "<prod id="690579815" lang="en" pre_order="no" web_offer="yes" in_stock="yes">product #1</prod>"
string(15) " id="690579815""
string(10) "product #1"
string(9) "690579815"
product #1
<prod id="690579815" lang="en" pre_order="no" web_offer="yes" in_stock="yes">product #1</prod>
<prod id="250544605" lang="en" pre_order="no" web_offer="yes" in_stock="yes">product #2</prod>
<prod id="360355798" lang="en" pre_order="no" web_offer="yes" in_stock="yes">product #3</prod>
Output for 5.1.2 - 5.1.6 , 5.2.0 - 5.2.17 , 5.3.0 - 5.3.29 , 5.4.0 - 5.4.45 , 5.5.24 - 5.5.35 , 5.6.7 - 5.6.28 , 7.0.0 - 7.0.20 , 7.1.0 - 7.1.25 NULL
int(0)
int(3)
string(94) "<prod id="690579815" lang="en" pre_order="no" web_offer="yes" in_stock="yes">product #1</prod>"
string(15) " id="690579815""
string(10) "product #1"
string(9) "690579815"
product #1
<prod id="690579815" lang="en" pre_order="no" web_offer="yes" in_stock="yes">product #1</prod>
<prod id="250544605" lang="en" pre_order="no" web_offer="yes" in_stock="yes">product #2</prod>
<prod id="360355798" lang="en" pre_order="no" web_offer="yes" in_stock="yes">product #3</prod>
Output for 5.0.0 - 5.0.5 , 5.1.0 - 5.1.1 NULL
int(0)
int(1)
string(94) "<prod id="690579815" lang="en" pre_order="no" web_offer="yes" in_stock="yes">product #1</prod>"
string(15) " id="690579815""
string(10) "product #1"
string(9) "690579815"
product #1
<prod id="690579815" lang="en" pre_order="no" web_offer="yes" in_stock="yes">product #1</prod>
<prod id="250544605" lang="en" pre_order="no" web_offer="yes" in_stock="yes">product #2</prod>
<prod id="360355798" lang="en" pre_order="no" web_offer="yes" in_stock="yes">product #3</prod>
Output for 4.4.5 - 4.4.9 Fatal error: Call to undefined function: simplexml_load_string() in /in/HuLc7 on line 16
Process exited with code 255 . Output for 4.3.2 - 4.3.11 , 4.4.0 - 4.4.1 , 4.4.3 - 4.4.4 Fatal error: Call to undefined function: simplexml_load_string() in /in/HuLc7 on line 16
Process exited with code 255 . Output for 4.4.2 Fatal error: Call to undefined function: simplexml_load_string() in /in/HuLc7 on line 17
Process exited with code 255 . Output for 4.3.0 - 4.3.1 Fatal error: Call to undefined function: simplexml_load_string() in /in/HuLc7 on line 16
preferences:dark mode live preview
218.37 ms | 401 KiB | 328 Q