I am using an XML parser to store the xml feed datas into mysql table and if I get an XML file result as an array, the result is shown below. How to store the each values into a variable.
Not this type of array only, If we use an different XML file or URL it’s result may be no of multidimensional arrays.
How to store the array values in php variables
<?php
function objectsIntoArray($arrObjData, $arrSkipIndices = array())
{
$arrData = array();
// if input is object, convert into 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); // recursive call
}
if (in_array($index, $arrSkipIndices)) {
continue;
}
$arrData[$index] = $value;
}
}
return $arrData;
}
$xmlUrl = "testxmel.xml"; // XML feed file/URL
$xmlStr = file_get_contents($xmlUrl);
$xmlObj = simplexml_load_string($xmlStr);
$arrXml = objectsIntoArray($xmlObj);
Testxmel.xml
<?xml version="1.0" encoding="utf-8"?>
<everyone>
<guest>
<name>Joseph Needham</name>
<age>53</age>
</guest>
<guest>
<name>Lu Gwei-djen</name>
<age>31</age>
</guest>
</everyone>
Anybody can help me to solve this XML parser result problem. Thanks in advance
Self-contained example using pdo
edit: given your xml you could do something like
output is the same.
But keep in mind that simplxml_load_file() keeps the whole DOM in memory. If your data source gets large that might become a problem and you better switch to XMLReadery,