0

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 ?

Ahmadz Issa
  • 669
  • 3
  • 12
  • 36
  • you can convert the xml to an array using the xml handler and function from php, after that you can work with the multidimensional array and handle your data in that array. – Broatcast Oct 25 '16 at 00:29
  • I converted the xml, but the different parents tags name gave me a headache, because same tags name in both data and data1 – Ahmadz Issa Oct 25 '16 at 00:38
  • how do you converted it and how does your array look now? then i can give you an advice how to handle your array ;) – Broatcast Oct 25 '16 at 00:40
  • I appreciate that, I edited my question. – Ahmadz Issa Oct 25 '16 at 00:47
  • now we can work with that ;) first dont search for author... search for the number tag to get a multidimensional array, then you can work in a foreach with the `$key as $value` trick to get seperate arrays with you can work with ... or write the arrays in a new multi array with new keys ;) – Broatcast Oct 25 '16 at 00:54
  • Sound easy, but hard to do ha ha. Thank you I will try to change it to control the key – Ahmadz Issa Oct 25 '16 at 01:09
  • An ugly solution to get into the deep of the array... you should look for a way with ksort and uksort... but i think you can see now what i mean and get your point for a solution ;) have a look at this [Snipped](https://3v4l.org/UXbFO) for you – Broatcast Oct 25 '16 at 01:39
  • Ohh nice example, it's really useful. I need many years to be as good as you. I will try tomorrow to work on your example, maybe I can't find a good solution. thanks ^^ – Ahmadz Issa Oct 25 '16 at 02:12
  • You did you ask the same question before - http://stackoverflow.com/questions/40220614/how-to-pull-specific-data-from-multidimensional-array This one shows no real improvement. Please read a little about the basics of parsing XML in PHP. – ThW Oct 25 '16 at 09:01

0 Answers0