您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

php 数组 xml json xml相互转换的方法

2025/6/25 18:52:24发布20次查看
首先,来看array->xml。$value) {首先,来看array->xml。$value) { if (is_array($value)) { $xml.=.ia2xml($value).$key>; } else { $xml.=.$value.$key>; } } return $xml; }
return simplexml_load_string(.ia2xml($array).$tag>); }
$test['type']='lunch'; $test['time']='12:30'; $test['menu']=array('entree'=>'salad', 'maincourse'=>'steak');
echo array2xml($test,meal)->asxml(); ?>
其次,xml->array。方法一:function xml2phparray($xml,$arr){ $iter = 0; foreach($xml->children() as $b){ $a = $b->getname(); if(!$b->children()){ $arr[$a] = trim($b[0]); }else{ $arr[$a][$iter] = array(); $arr[$a][$iter] = xml2phparray($b,$arr[$a][$iter]); $iter++; } } return $arr; }
$xml = georgejohnreminderdon't forget the meeting!ccceeexml;
print_r(xml2phparray(simplexml_load_string ( $xml ),array()));
方法二:function xml2array ( $xml , $recursive = false ){ if ( ! $recursive ) { $array = simplexml_load_string ( $xml ) ; } else { $array = $xml ; } $newarray = array () ; $array = ( array ) $array ; foreach ( $array as $key => $value ) { $value = ( array ) $value ; if ( isset ( $value [ 0 ] ) ) { $newarray [ $key ] = trim ( $value [ 0 ] ) ; } else { $newarray [ $key ] = xml2array ( $value , true ) ; } } return $newarray ;}
$xml = georgejohnreminderdon't forget the meeting!ccceeexml;
print_r(xml2array($xml));
来看json->array。json_decode($json,true);//第二个参数为true时 即为array$json = '{a:1,b:2,c:3,d:4,e:5}';var_dump(json_decode($json));var_dump(json_decode($json, true));
最后,来看array->jsonjson_encode 数组->json$arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);echo json_encode($arr);
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product