[ create a new paste ] login | about

Link: http://codepad.org/KKndkXEo    [ raw code | output | fork ]

PHP, pasted on Mar 15:
<?php

    $xmlData = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
     <configdata>
        <records>
             <somekey>
                <record>
                    <firstname>Jonathan</firstname>
                    <lastname>Kushner</lastname>
                </record>
                <record>
                    <firstname>Dustin</firstname>
                    <lastname>Kushner</lastname>
                </record>
                <record>
                    <firstname>Cameron</firstname>
                    <lastname>Kushner</lastname>
                </record>
            </somekey>
        </records>
    </configdata>
XML;

    // Create the document object
    $xml = simplexml_load_string($xmlData);

    $result = array();

    // Get the <record> nodes and loop them
    foreach ($xml->xpath('//record') as $record) {
        $result[] = array(
            'firstname' => (string) $record->firstname,
            'lastname' => (string) $record->lastname,
        );
    }

    print_r($result);


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Array
(
    [0] => Array
        (
            [firstname] => Jonathan
            [lastname] => Kushner
        )

    [1] => Array
        (
            [firstname] => Dustin
            [lastname] => Kushner
        )

    [2] => Array
        (
            [firstname] => Cameron
            [lastname] => Kushner
        )

)


Create a new paste based on this one


Comments: