下記のようなSimpleXMLElement型に変換したXMLの各要素へのアクセス方法がよくわかりません。
XML
1 object(SimpleXMLElement)#4 (2) { 2 ["@attributes"]=> 3 array(1) { 4 ["date"]=> 5 string(10) "2018-05-02" 6 } 7 ["Value"]=> 8 string(5) "28548" 9 }
$date="2018-05-02"と$value='28548'のように別の変数に格納したいのですが、アクセスの仕方がよくわからなくて困ってます。
上記のSimpleXMLElementオブジェクトを$xmlに格納したとすると、
PHP
1$date = $xml['date']; 2$value = $xml->Value;
でできると思ったのですが、できませんでした。。。
追記:
XMLの原文は、
XML
1<Result date="2018-05-02"> 2<Value label="label" key="key">14418</Value> 3</Result>
です。
冒頭に書いたオブジェクトは、
PHP
1$file = simplexml_load_file('xmldata.xml'); 2$xml = $file->Result;
で、パースして取得した$xmlです。(xmldata.xmlにxmlが記載されています。)
追記その2:
PHP
1$data_set = [ 2 'date' => $xml->attributes()->date, 3 'value' => $xml->Value, 4];
のようにして取得した物をvar_dump()したら、
PHP
1array(2) { 2 ["date"]=> 3 object(SimpleXMLElement)#10 (1) { 4 [0]=> 5 string(10) "2018-05-02" 6 } 7 ["value"]=> 8 object(SimpleXMLElement)#5 (2) { 9 ["@attributes"]=> 10 array(2) { 11 ["label"]=> 12 string(5) "label" 13 ["key"]=> 14 string(3) "key" 15 } 16 [0]=> 17 string(5) "14418" 18 } 19}
となってました。
理想の出力結果としては、
PHP
1array(2) { 2 ["date"]=> 3 string(10) "2018-05-02" 4 ["value"]=> 5 string(5) "14418" 6}
となってほしいです。
回答1件
あなたの回答
tips
プレビュー