php 数据处理相关代码片段

一.2V数组转树形 代码片段

 

数据模型

 

$items = array(
1 => array(‘id’ => 1, ‘pid’ => 0, ‘name’ => ‘江西省’),
2 => array(‘id’ => 2, ‘pid’ => 0, ‘name’ => ‘黑龙江省’),
3 => array(‘id’ => 3, ‘pid’ => 1, ‘name’ => ‘南昌市’),
4 => array(‘id’ => 4, ‘pid’ => 2, ‘name’ => ‘哈尔滨市’),
5 => array(‘id’ => 5, ‘pid’ => 2, ‘name’ => ‘鸡西市’),
6 => array(‘id’ => 6, ‘pid’ => 4, ‘name’ => ‘香坊区’),
7 => array(‘id’ => 7, ‘pid’ => 4, ‘name’ => ‘南岗区’),
8 => array(‘id’ => 8, ‘pid’ => 6, ‘name’ => ‘和兴路’),
9 => array(‘id’ => 9, ‘pid’ => 7, ‘name’ => ‘西大直街’),
10 => array(‘id’ => 10, ‘pid’ => 8, ‘name’ => ‘东北林业大学’),
11 => array(‘id’ => 11, ‘pid’ => 9, ‘name’ => ‘哈尔滨工业大学’),
12 => array(‘id’ => 12, ‘pid’ => 8, ‘name’ => ‘哈尔滨师范大学’),
13 => array(‘id’ => 13, ‘pid’ => 1, ‘name’ => ‘赣州市’),
14 => array(‘id’ => 14, ‘pid’ => 13, ‘name’ => ‘赣县’),
15 => array(‘id’ => 15, ‘pid’ => 13, ‘name’ => ‘于都县’),
16 => array(‘id’ => 16, ‘pid’ => 14, ‘name’ => ‘茅店镇’),
17 => array(‘id’ => 17, ‘pid’ => 14, ‘name’ => ‘大田乡’),
18 => array(‘id’ => 18, ‘pid’ => 16, ‘name’ => ‘义源村’),
19 => array(‘id’ => 19, ‘pid’ => 16, ‘name’ => ‘上坝村’),
);

函数

 

function genTree9(Array $items) {

$tree = array();    //格式化好的树
foreach ($items as $item)
if (isset($items[$item[‘pid’]]))
$items[$item[‘pid’]][‘son’][] = &$items[$item[‘id’]];
else
$tree[] = &$items[$item[‘id’]];
return $tree;

}

二.传输过程中 汉字不转uncode

json_encode($data, JSON_UNESCAPED_UNICODE)

三.带有特殊字符的url转码

 

urlencode($data)

 

如  http://weixin.com?XXXX?a=1&b=222&c=666/d=777/…

 

其中? & /将被转义

四.get和post请求

get

function get_curl($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
//运行curl,结果以json形式返回
$res = curl_exec($ch);
curl_close($ch);
return $res;
}

//post请求
function http_request($url,$data){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output=curl_exec($curl);
curl_close($curl);
return $output;
}

file_get_content()函数也可以作get获取内容 亦可以用作读取网络文件

《php 数据处理相关代码片段》有1个想法

  1. //–获取签名

    function get_sign($data,$key){
    ksort($data);
    $str = array();
    foreach($data as $k=>$v){
    $str[] = $k.’=’.$v;
    }
    $str = implode(‘&’,$str);
    $str .=’&key=’.$key;
    $str = md5($str);
    $str = strtoupper($str);
    return $str;
    }

    //–数组转xml

    function arrayToXml($arr){
    $xml = “”;
    foreach ($arr as $key=>$val){
    if(is_array($val)){
    //$xml.=””.arrayToXml($val).””;
    $xml.=””;
    }else{
    //$xml.=””.$val.””;
    $xml.=””;
    }
    }
    $xml.=””;
    return $xml;
    }

    function xmlToArray($xml){

    //禁止引用外部xml实体

    libxml_disable_entity_loader(true);

    $xmlstring = simplexml_load_string($xml, ‘SimpleXMLElement’, LIBXML_NOCDATA);

    $val = json_decode(json_encode($xmlstring),true);

    return $val;

    }

发表评论

电子邮件地址不会被公开。 必填项已用*标注