Reputation: 2466
I have a xml response which has the following nodes in it
$response = '<packet version="1.6.6.0">
<webspace>
<del>
<result>
<status>ok</status>
<filter-id>14</filter-id>
<id>14</id>
</result>
</del>
</webspace>
</packet>';
I want to grab the value of the node <filter-id>
in a variable.
I am doing the following,
$dbres = simplexml_load_string($response);
$filterid = $dbres->webspace->result->filter-id;
echo $filterid;
I am getting error unexpected T_OBJECT_OPERATOR
which I think indicated the hyphen "-" in filter-id
. I cannot change the xml as it is a response from API. How can I get the filter-id ?
Upvotes: 0
Views: 39
Reputation: 2815
Use brackets:
$dbres = simplexml_load_string($response);
$filterid = $dbres->webspace->result->{filter-id};
echo $filterid;
Or cast it:
$dbres = simplexml_load_string($response);
$filterid = ((array) $dbres->webspace->result)['filter-id'];
echo $filterid;
Or - a bit weird:
$dbres = simplexml_load_string(str_replace("filter-id", "filterid", $response));
$filterid = $dbres->webspace->result->filterid;
echo $filterid;
Upvotes: 2