「Php/xml/SimpleXMLElement」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→配列からxml生成) |
|||
| 行10: | 行10: | ||
} | } | ||
echo $xml->asXML(); | echo $xml->asXML(); | ||
| + | |||
| + | ==xmlから配列へ== | ||
| + | $xml = simplexml_load_string($resxml); | ||
| + | $json = json_encode($xml); | ||
| + | $array = json_decode($json, TRUE); | ||
| + | print_r($array); | ||
2017年11月30日 (木) 18:39時点における版
配列からxml生成
$xmlstr = "<?xml version='1.0' encoding='utf-8'?><tags></tags>";
$xml = new SimpleXMLElement($xmlstr);
$tags = [
'name' => 'taro',
'age' => 1
];
foreach($tags as $key => $value) {
$xml->addChild($key, $value);
}
echo $xml->asXML();
xmlから配列へ
$xml = simplexml_load_string($resxml); $json = json_encode($xml); $array = json_decode($json, TRUE); print_r($array);
