I'm trying to split an XML in the array, below is my php codes:
<?php
$xml = simplexml_load_file("test.xml") or die("Error: Cannot create object");
foreach($xml->children() as $books) {
echo $books->title . "<br> ";
echo $books->tutor . "<br> ";
echo $books->duration . "<br> ";
echo $books->price . "<hr>";
}
?>
Below is my XML codes:
<?xml version = "1.0" encoding = "utf-8"?>
<tutorialspoint>
<course category = "JAVA">
<title lang = "en">Java</title>
<tutor>Gopal</tutor>
<duration>3</duration>
<price>$30</price>
</course>
<course category = "HADOOP">
<title lang = "en">Hadoop</title>.
<tutor>Satish</tutor>
<duration>3</duration>
<price>$50</price>
</course>
<course category = "HTML">
<title lang = "en">html</title>
<tutor>raju</tutor>
<duration>5</duration>
<price>$50</price>
</course>
<course category = "WEB">
<title lang = "en">Web Technologies</title>
<tutor>Javed</tutor>
<duration>10</duration>
<price>$60</price>
</course>
</tutorialspoint>
But it gave the output show me in below: enter image description here
I want to convert XML into the array with PHP, but can't work. Actually the output I want like in below example codes:
Array
(
[0] => Array
(
[title] => Java
[tutor] => Gopal
[duration] => 3
[price] => $30
)
[1] => Array
(
[title] => Hadoop
[tutor] => Satish
[duration] => 3
[price] => $50
)
[2] => Array
(
[title] => HTML
[tutor] => raju
[duration] => 5
[price] => $50
)
[3] => Array
(
[title] => Web Technologies
[tutor] => Javed
[duration] => 10
[price] => $60
)
I don't know how to set them into the array like above the example output. Hope someone can help me. Thanks.
$books[] = [ 'title' => $books->title, 'tutor' => $books->tutor...instead of yourecho ..., don't forget to create a blank array first.