getting error while converting xml to json-Collection of common programming errors
I am getting undefined function error when i am using json_encode()
function. I need to convert the xml data to json data. The code i am using is :
$xml = simplexml_load_string($retValue);
echo json_encode($xml);
I also tried this:
$xml = simplexml_load_string($retValue);
$array = objectsIntoArray($xml);
$array = json_encode($array);
print_r($array);
die;
function objectsIntoArray($arrObjData, $arrSkipIndices = array())
{
$arrData = array();
if (is_object($arrObjData)) {
$arrObjData = get_object_vars($arrObjData);
}
if (is_array($arrObjData)) {
foreach ($arrObjData as $index => $value) {
if (is_object($value) || is_array($value)) {
$value = objectsIntoArray($value, $arrSkipIndices);
}
if (in_array($index, $arrSkipIndices)) {
continue;
}
$arrData[$index] = $value;
}
}
return $arrData;
}
But in this objectsIntoArray()
and json_encode()
both are throwing function undefined errors. $retValue
contains the xml data. I am not able to figure out why i am getting this error. Am i missing something. I am totally new to php and not much aware of it. Please help me regarding this.
Originally posted 2013-11-09 21:26:45.