Getting values from SimpleXMLElement or SimpleType with hypen in php

by rajesh 2012-05-11 00:47:49

Consider $responseXml is a SimpleXMLElement with contains certain nodes with hypen "-" in its name

The following code will not work
foreach ($responseXml->xpath('/path/result') as $resultNode) {
$cid = $resultNode->id;
$cname = (string)$resultNode->data->gen_info->c-name;
}

We will always get $cname as 0

The fix is using {'...'}
$cname = (string)$resultNode->data->gen_info->{'c-name'};

Tagged in:

1373
like
0
dislike
0
mail
flag

You must LOGIN to add comments