⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 arraytojson.php

📁 array to json php code version 1
💻 PHP
字号:
<?phpfunction array2json($arr) {    if(function_exists('json_encode')) return json_encode($arr); //Lastest versions of PHP already has this functionality.    $parts = array();    $is_list = false;    //Find out if the given array is a numerical array    $keys = array_keys($arr);    $max_length = count($arr)-1;    if(($keys[0] == 0) and ($keys[$max_length] == $max_length)) {//See if the first key is 0 and last key is length - 1        $is_list = true;        for($i=0; $i<count($keys); $i++) { //See if each key correspondes to its position            if($i != $keys[$i]) { //A key fails at position check.                $is_list = false; //It is an associative array.                break;            }        }    }    foreach($arr as $key=>$value) {        if(is_array($value)) { //Custom handling for arrays            if($is_list) $parts[] = array2json($value); /* :RECURSION: */            else $parts[] = '"' . $key . '":' . array2json($value); /* :RECURSION: */        } else {            $str = '';            if(!$is_list) $str = '"' . $key . '":';            //Custom handling for multiple data types            if(is_numeric($value)) $str .= $value; //Numbers            elseif($value === false) $str .= 'false'; //The booleans            elseif($value === true) $str .= 'true';            else $str .= '"' . addslashes($value) . '"'; //All other things            // :TODO: Is there any more datatype we should be in the lookout for? (Object?)            $parts[] = $str;        }    }    $json = implode(',',$parts);        if($is_list) return '[' . $json . ']';//Return numerical JSON    return '{' . $json . '}';//Return associative JSON} function get_json($data=null){if($data){if(is_array($data)){$json='';foreach($data as $k=>$v){if($ret_arr = is_numeric($k)){$json .= get_json($v).",";}else{$json .= "$k:".get_json($v).",";}}$json = substr($json,0,-1);$json = ($ret_arr)? "[$json]" : "{".$json."}" ;return $json;}elseif(is_string($data))return '"'.$data.'"';elseif(is_numeric($data))return $data;elseif(is_bool($data))return ($data)?"true":"false";}return $data;}$arr ='2|2|1|Array(    [your_name] => Anne  Parker    [phone] => 416-391-0773    [postal] => M3C    [a15] =>     [a0] =>     [a1] =>     [a2] =>     [a3] =>     [a4] =>     [a5] =>     [a6] =>     [a7] =>     [a8] =>     [a9] =>     [a10] =>     [a11] =>     [a12] =>     [a13] =>     [a14] =>     [b0] =>     [b1] =>     [b2] =>     [b3] =>     [b4] =>     [b5] =>     [b6] =>     [b7] =>     [b8] =>     [b9] =>     [b10] =>     [b11] =>     [b12] =>     [c0] =>     [c1] =>     [c2] =>     [c3] =>     [c4] =>     [c5] =>     [c6] =>     [c7] =>     [c8] =>     [c9] =>     [c10] =>     [d0] =>     [d1] =>     [d2] =>     [d3] =>     [d4] =>     [d5] =>     [d6] =>     [d7] =>     [d8] =>     [d9] =>     [d10] =>     [d11] =>     [d12] =>     [d13] =>     [d14] =>     [d15] =>     [d16] =>     [d17] =>     [d18] =>     [d19] =>     [d20] =>     [d21] =>     [e0] =>     [e1] =>     [e2] => Cellar Door Winery    [f0] =>     [f1] =>     [f2] =>     [f3] =>     [f4] =>     [f5] =>     [f6] =>     [f7] =>     [f8] =>     [f9] =>     [f10] =>     [f11] =>     [f12] =>     [f13] =>     [favs] =>     [proccess] => step1    [__utma] => 259845775.3762399144909638700.1218129813.1218129813.1218129813.1    [__utmb] => 259845775.5.10.1218129813    [__utmc] => 259845775    [__utmz] => 259845775.1218129813.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none))|step2|259845775.3762399144909638700.1218129813.1218129813.1218129813.1|259845775.5.10.1218129813|259845775|259845775.1218129813.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)';$test = array2json($arr);echo $test;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -