web-dev-qa-db-ja.com

SimpleXMLオブジェクトをPHP配列に変換する方法は?

次のコードについて考えてみます。

$string = '<device>
    <id>1234</id>
    <label>118</label>
    <username>root</username>
    <password>helloWorld</password>
    <hardware>
        <memory>4GB RAM</memory>
        <storage_drives>
            <storage_drive_1>2TB SATA 7,200RPM</storage_drive_1>
            <storage_drive_2>1TB SATA 7,200RPM</storage_drive_2>
            <storage_drive_3>Not Applicable</storage_drive_3>
            <storage_drive_4>Not Applicable</storage_drive_4>
        </storage_drives>
    </hardware>
</device>';
$xml = new SimpleXMLElement($string);

$deviceDetails = Array();
foreach($xml as $element){
    $tag = $element->getName();
    $deviceDetails +=  Array($tag => '$element->$tag)',
        );
    }

出力$detailsDetails配列は次のとおりです。

Array
(
    [id] => $element->$tag)
    [label] => $element->$tag)
    [username] => $element->$tag)
    [password] => $element->$tag)
    [hardware] => $element->$tag)
)

それは間違っています。

私の質問は、$element->$tagを機能させる方法は?

16
CuriousMind

Book Of Zeus再帰的に機能させるために関数でラップされたコード:

function xml2array($xml)
{
    $arr = array();

    foreach ($xml as $element)
    {
        $tag = $element->getName();
        $e = get_object_vars($element);
        if (!empty($e))
        {
            $arr[$tag] = $element instanceof SimpleXMLElement ? xml2array($element) : $e;
        }
        else
        {
            $arr[$tag] = trim($element);
        }
    }

    return $arr;
}

$xml = new SimpleXMLElement($string);
print_r(xml2array($xml));

Array
(
    [id] => 1234
    [label] => 118
    [username] => root
    [password] => helloWorld
    [hardware] => Array
    (
        [memory] => 4GB RAM
        [storage_drives] => Array
        (
            [storage_drive_1] => 2TB SATA 7,200RPM
            [storage_drive_2] => 1TB SATA 7,200RPM
            [storage_drive_3] => Not Applicable
            [storage_drive_4] => Not Applicable
        )
    )
)
15
dfsq

これを試して:

$string = '<device>
  <id>1234</id>
  <label>118</label>
  <username>root</username>
  <password>helloWorld</password>
  <hardware>
    <memory>4GB RAM</memory>
     <storage_drives>
      <storage_drive_1>2TB SATA 7,200RPM</storage_drive_1>
      <storage_drive_2>1TB SATA 7,200RPM</storage_drive_2>
      <storage_drive_3>Not Applicable</storage_drive_3>
      <storage_drive_4>Not Applicable</storage_drive_4>
    </storage_drives>
  </hardware>
</device>';

$xml = json_decode(json_encode((array) simplexml_load_string($string)), 1);

これは出力します:

Array
(
    [id] => 1234
    [label] => 118
    [username] => root
    [password] => helloWorld
    [hardware] => Array
        (
            [memory] => 4GB RAM
            [storage_drives] => Array
                (
                    [storage_drive_1] => 2TB SATA 7,200RPM
                    [storage_drive_2] => 1TB SATA 7,200RPM
                    [storage_drive_3] => Not Applicable
                    [storage_drive_4] => Not Applicable
                )

        )

)

または、これが気に入らない場合は、PHPクラスのように: http://www.bin-co.com/php/scripts/xml2array/ ==

または表示dfsq回答

16
Book Of Zeus

これを試して:

$array = json_decode(json_encode((array)$xml), TRUE);
4