zoukankan      html  css  js  c++  java
  • php 数组转xml 数组转json xml转数组 json转数组

    array->xml

    <?php function array2xml($array, $tag) {
        function ia2xml($array) {         $xml="";         foreach ($array as $key=>$value) {             if (is_array($value)) {                 $xml.="<$key>".ia2xml($value)."</$key>";             } else {                 $xml.="<$key>".$value."</$key>";             }         }         return $xml;     }
        return simplexml_load_string("<$tag>".ia2xml($array)."</$tag>"); }
    $test['type']='lunch'; $test['time']='12:30'; $test['menu']=array('entree'=>'salad', 'maincourse'=>'steak');
    echo array2xml($test,"meal")->asXML(); ?>

    xml->array

    method1:

    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 = <<<XML <?xml version="1.0" encoding="ISO-8859-1"?> <note> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget the meeting!</body> <a><c>ccc</c><e>eee</e></a> </note> XML;
    print_r(xml2phpArray(simplexml_load_string ( $xml ),array()));

    method2:

    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 = <<<XML <?xml version="1.0" encoding="ISO-8859-1"?> <note> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget the meeting!</body> <a><b><c>ccc</c></b><e>eee</e></a> </note> XML;
    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->json

    json_encode 数组-》json

    $arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);
    echo
    json_encode($arr);

    现主要从事PHP、Uinx/Linux、C/C++等方面的项目开发。
  • 相关阅读:
    JSON特殊字符的处理
    java中高并发和高响应解决方法
    对redis深入理解
    对java中arraylist深入理解
    Redis的字典扩容与ConcurrentHashMap的扩容策略比较
    PHP压缩上传图片
    windows 平台 php_Imagick 拓展遇到的那些坑!
    windows7下php5.4成功安装imageMagick,及解决php imagick常见错误问题。(phpinfo中显示不出来是因为:1.imagick软件本身、php本身、php扩展三方版本要一致,2.需要把CORE_RL_*.dll多个文件放到/php/目录下面)
    php使用imagick模块实现图片缩放、裁剪、压缩示例
    文件打包,下载之使用PHP自带的ZipArchive压缩文件并下载打包好的文件
  • 原文地址:https://www.cnblogs.com/lsl8966/p/2519668.html
Copyright © 2011-2022 走看看