本文主要介绍php中php与xml和数组的互相转换方法,包括从xml到数组、从数组到xml的转换方法。
一、php与xml的互相转换
在php中,我们可以使用simplexml扩展来实现php与xml的互相转换。simplexml是一个php的内置扩展,用于处理xml文件中的数据,可以将xml文件转换为php对象或数组,也可以将php对象或数组转换为xml文件。
将xml文件转换为php数组将xml文件转换为php数组,需要使用simplexml扩展中的simplexml_load_file函数。simplexml_load_file函数可以将指定路径的xml文件读取并返回一个simplexmlelement对象,然后通过对simplexmlelement对象的遍历,将xml文件中的信息转换为php数组。
下面是一个将xml文件转换为php数组的代码示例:
$xmlstr = <<<xml<?xml version='1.0'?><menu> <item> <name>chicken curry</name> <price>8.95</price> <description>a spicy dish with chicken, carrots, and potatoes</description> </item> <item> <name>beef stir fry</name> <price>10.95</price> <description>a savory dish with beef, mushrooms, and peppers</description> </item></menu>xml;$xml = simplexml_load_string($xmlstr);$menu = [];foreach ($xml->item as $item) { $menu[] = [ 'name' => (string) $item->name, 'price' => (float) $item->price, 'description' => (string) $item->description, ];}print_r($menu);
运行以上代码,输出的结果为:
array( [0] => array ( [name] => chicken curry [price] => 8.95 [description] => a spicy dish with chicken, carrots, and potatoes ) [1] => array ( [name] => beef stir fry [price] => 10.95 [description] => a savory dish with beef, mushrooms, and peppers ))
将php数组转换为xml文件将php数组转换为xml文件,需要将数组中的数据按照xml的语法规则进行组织。在php中,我们可以使用simplexmlelement类来创建xml元素,并使用foreach循环来遍历数组中的数据,将数据逐个添加到xml元素中,最后通过simplexmlelement对象的asxml方法将xml文件输出或保存到指定位置。
下面是一个将php数组转换为xml文件的代码示例:
$menu = [ [ 'name' => 'chicken curry', 'price' => 8.95, 'description' => 'a spicy dish with chicken, carrots, and potatoes' ], [ 'name' => 'beef stir fry', 'price' => 10.95, 'description' => 'a savory dish with beef, mushrooms, and peppers' ]];$xml = new simplexmlelement('<menu></menu>');foreach ($menu as $item) { $menuitem = $xml->addchild('item'); $menuitem->addchild('name', $item['name']); $menuitem->addchild('price', $item['price']); $menuitem->addchild('description', $item['description']);}$xmlstr = $xml->asxml();echo $xmlstr;
运行以上代码,输出的结果为:
<?xml version="1.0"?><menu> <item> <name>chicken curry</name> <price>8.95</price> <description>a spicy dish with chicken, carrots, and potatoes</description> </item> <item> <name>beef stir fry</name> <price>10.95</price> <description>a savory dish with beef, mushrooms, and peppers</description> </item></menu>
二、php与数组的互相转换
在php中,我们可以使用json_encode和json_decode函数来实现php与数组的互相转换。json_encode函数将php中的数组转换为json格式的字符串,并返回转换后的字符串;json_decode函数将json格式的字符串转换为php中的数组。
将php数组转换为json格式的字符串将php数组转换为json格式的字符串,需要使用json_encode函数。json_encode函数可以将php中的数组转换为json格式的字符串,并返回一个表示json字符串的值。
下面是一个将php数组转换为json格式的字符串的代码示例:
$menu = [ [ 'name' => 'chicken curry', 'price' => 8.95, 'description' => 'a spicy dish with chicken, carrots, and potatoes' ], [ 'name' => 'beef stir fry', 'price' => 10.95, 'description' => 'a savory dish with beef, mushrooms, and peppers' ]];$json = json_encode($menu);echo $json;
运行以上代码,输出的结果为:
[{name:chicken curry,price:8.95,description:a spicy dish with chicken, carrots, and potatoes},{name:beef stir fry,price:10.95,description:a savory dish with beef, mushrooms, and peppers}]
将json格式的字符串转换为php数组将json格式的字符串转换为php数组,需要使用json_decode函数。json_decode函数可以将json格式的字符串转换为php中的数组,并返回一个表示php数组的值。
下面是一个将json格式的字符串转换为php数组的代码示例:
$json = '[{name:chicken curry,price:8.95,description:a spicy dish with chicken, carrots, and potatoes},{name:beef stir fry,price:10.95,description:a savory dish with beef, mushrooms, and peppers}]';$menu = json_decode($json, true);print_r($menu);
运行以上代码,输出的结果为:
array( [0] => array ( [name] => chicken curry [price] => 8.95 [description] => a spicy dish with chicken, carrots, and potatoes ) [1] => array ( [name] => beef stir fry [price] => 10.95 [description] => a savory dish with beef, mushrooms, and peppers ))
三、php与xml数组的互相转换
在php中,我们可以使用simplexml扩展和json_encode/json_decode函数来实现php与xml和数组的互相转换。具体地,我们可以先将xml文件转换为php数组,然后将php数组转换为json格式的字符串;同样地,我们也可以将json格式的字符串转换为php数组,然后将php数组转换为simplexmlelement对象,最终得到xml文件。
下面是一个完整的php与xml数组的互相转换的代码示例:
$xmlstr = <<<xml<?xml version='1.0'?><menu> <item> <name>chicken curry</name> <price>8.95</price> <description>a spicy dish with chicken, carrots, and potatoes</description> </item> <item> <name>beef stir fry</name> <price>10.95</price> <description>a savory dish with beef, mushrooms, and peppers</description> </item></menu>xml;// 将xml文件转换为php数组$xml = simplexml_load_string($xmlstr);$menu = [];foreach ($xml->item as $item) { $menu[] = [ 'name' => (string) $item->name, 'price' => (float) $item->price, 'description' => (string) $item->description, ];}// 将php数组转换为json格式的字符串$json = json_encode($menu);// 将json格式的字符串转换为php数组$menu = json_decode($json, true);// 将php数组转换为simplexmlelement对象$xml = new simplexmlelement('<menu></menu>');foreach ($menu as $item) { $menuitem = $xml->addchild('item'); $menuitem->addchild('name', $item['name']); $menuitem->addchild('price', $item['price']); $menuitem->addchild('description', $item['description']);}// 输出xml文件$xmlstr = $xml->asxml();echo $xmlstr;
运行以上代码,输出的结果为:
<?xml version="1.0"?><menu> <item> <name>chicken curry</name> <price>8.95</price> <description>a spicy dish with chicken, carrots, and potatoes</description> </item> <item> <name>beef stir fry</name> <price>10.95</price> <description>a savory dish with beef, mushrooms, and peppers</description> </item></menu>
总结
以上就是php与xml和数组的互相转换的方法,非常简单实用。无论是在前后端的数据传输还是数据处理中,我们都可以根据实际需要进行对应的转换。需要注意的是,在进行xml和数组之间的转换时,要注意数据的结构和格式,以免出现不必要的错误。
以上就是php+xml和数组怎么互相转换的详细内容。
