This link above has nothing to do with my question OMG I want to assign each node to a variable so I can control the data to insert them in the database.
Example of XML File
<?xml version="1.0"?>
<catalog>
<book>
<took>
<cl>
<shp>
<number>
<data>
<author>jack</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies.</description>
</data>
<data1>
<author>kirito</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies.</description>
</data1>
</number>
</shp>
</cl>
</took>
</book>
</catalog>
as you see there are and that have the same child tags, but different parents tag name
How can I pull the data into a variable in order to insert them into the database
I almost read everything on the Internet, but all of them are using attributes!.
------------- Edited
I tried to convert the XML to array and this's the results
Array
(
[catalog] => Array
(
[book] => Array
(
[took] => Array
(
[cl] => Array
(
[shp] => Array
(
[number] => Array
(
[data] => Array
(
[author] => jack
[title] => Midnight Rain
[genre] => Fantasy
[price] => 5.95
[publish_date] => 2000-12-16
[description] => A former architect battles corporate zombies.
)
[data1] => Array
(
[author] => Kirito
[title] => Midnight Rain
[genre] => Fantasy
[price] => 5.95
[publish_date] => 2000-12-16
[description] => A former architect battles corporate zombies.
)
)
)
)
)
)
)
)
I did that by using this code
function XML2Array(SimpleXMLElement $parent)
{
$array = array();
foreach ($parent as $name => $element) {
($node = & $array[$name])
&& (1 === count($node) ? $node = array($node) : 1)
&& $node = & $node[];
$node = $element->count() ? XML2Array($element) : trim($element);
}
return $array;
}
$xml = simplexml_load_file('lol.xml');
$array = XML2Array($xml);
$array = array($xml->getName() => $array);
And this code to pull out the data
$author_array = array();
array_walk_recursive($array, function($value, $key) {
if (in_array($key, array("author"))) {
echo $author_array[] = $value;
}
});
but the problem that it returns both authors
jack and kirito
I want to display one data depends on the key data or data1
How can I do that ?