Sariel Sariel - 1 year ago 146
PHP Question

simplexml get value not all attributes

i am not very familiar with php (just start learning) and i have a problem:
i have a xml file on a server (from eve ... but who cares) and i managed it to "read" this file. it has a structure like this:

<row name="abc" id="def" ...>
<row name="abc" id="def" ...>
<row name="abc" id="def" ...>

searching some web pages i found a way to echo all attributes of all rows.
here is my code:

$url = "";

$xml = simplexml_load_file($url);

$nrOfRows = $xml->result[0]->rowset[0]->count();
$n = 0;

for ($n = 0; $n <= $nrOfRows; $n++) {
foreach($xml->result[0]->rowset[0]->row[$n]->attributes() as $a => $b) {
echo $a, " = ", $b, "<br>";

as a result i get:

name = abc
id = def
name = abc
id = def

this looks nice but thats not exactly what i want.
i only need a special attribute of all rows. for example i want all the names of all rows as result.

thanks for any help.

Answer Source

You can use array syntax to access to a single attribute:

echo $xml->result[0]->rowset[0]->row[$n]['name'];

will output:

Goonswarm Federation
Pandemic Horde
Test Alliance Please Ignore

Also note that you can simplify your code in this way:

foreach( $xml->result[0]->rowset[0]->row as $row )
    echo $row['name'] . '<br>';
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download