range

If there were you, the world would be just right

function getSize($size) {
    $units = array(' B', ' KB', ' MB', ' GB', ' TB');
    for ($i = 0; $size >= 1024 && $i < 4; $i++) $size /= 1024;
    return round($size, 2).$units[$i];
}

方便复制


function curl_get($url, $is_ssl = false, $headers = [])
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    if ($headers) {
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    }
    if ($is_ssl) {
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    }
    $data = curl_exec($curl);
    curl_close($curl);
    return $data;
}

UPDATE ucenter_user_money a, (select user_id,openid,unionid from ucenter_user_details where openid!='') b SET a.openid = b.openid,a.unionid=b.unionid WHERE a.user_id = b.user_id;

批量更新
UPDATE yisdk_device a, ( SELECT uuid, channel, pf_game_id, utma, MD5( CONCAT( "new_", CONCAT_WS( "_", channel, pf_game_id, utma ))) AS new_uuid FROM yisdk_device WHERE addtime >= '1588262400' AND uuid != MD5( CONCAT( "new_", CONCAT_WS( "_", channel, pf_game_id, utma )))) b SET a.uuid = b.new_uuid WHERE a.channel = b.channel and a.pf_game_id = b.pf_game_id and a.utma = b.utma


1.xml转数组

function xmlToArray( $xml )
{
    if ( !$xml ) {
        return false;
    }
    //禁止引用外部xml实体
    libxml_disable_entity_loader(true);
    // 将XML转为array
    $data = json_decode( json_encode( simplexml_load_string( $xml, 'SimpleXMLElement', LIBXML_NOCDATA ) ), true );
    return $data;
}

2.数组转xml

function array_toxml($array)
{
    if (!is_array($array) || count($array) <= 0) {
        return false;
    }

    $xml = "<xml>";
    foreach ($array as $key => $val) {
        if (is_numeric($val)) {
            $xml .= "<" . $key . ">" . $val . "</" . $key . ">";
        } else {
            $xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
        }
    }
    $xml .= "</xml>";
    return $xml;
}

无限级应用场景:下拉列表,树型列表等

多种实现方式

第一种(推荐)

function infiniteSort($data, $showFName, $titleFName, $pidFName = 'pid', $idFName = 'id', $levelFName = 'level', $pid = 0, $level = 0)
{
    $tree = array();

    foreach ($data as $key => $value) {

        if ($value[$pidFName] == $pid) {
            $value[$levelFName] = $level;
            $value[$showFName] = str_repeat('&nbsp;&nbsp;', $level) . '|-' . $value[$titleFName];
            $tree[] = $value;
            unset($data[$key]);
            $tempArr = infiniteSort($data, $showFName, $titleFName, $pidFName, $idFName, $level, $value[$idFName], $level + 1);
            if(!empty($tempArr)){
                $tree = array_merge($tree, $tempArr);
            }
        }

    }

    return $tree;
}
注意:
1、$data 已经asc排序过的所有数据
2、$showFName 显示名字的字段名(格式化过的)
3、$titleFName 标题的字段名(无格式化)
4、$levelFName 层级字段名
5、$pidFName 父id的字段名
6、$idFName id的字段名

第二种(使用引用变量)

/**
 * 无限级分类
 * @param Array $treeList //接受处理完成数据的数组
 * @param Array $data //数据库里获取的结果集
 * @param String $level //格式化层级字段名
 * @param Int $pid
 * @param Int $count //第几级分类
 */
function tree(&$treeList, &$data, $level, $show_name, $field_name, $field_pid = 'pid', $field_id = 'id', $pid = 0, $count = 0)
{
    foreach ($data as $key => $value) {

        if ($value[$field_pid] == $pid) {
            $value[$level] = $count;
            $value[$show_name] = str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;',$count).'|-'.$value[$field_name];
            $treeList[] = $value;
            unset($data[$key]);
            tree($treeList, $data, $level, $show_name, $field_name,$field_pid, $field_id, $value[$field_id], $count+1);
        }

    }
}
注意:
1、$data 已经asc排序过的所有数据
2、返回的无限级列表数据都存在$treeList里面

第三种(使用静态变量有限制:如果一次请求调用两次来实现2个无限级分类就会出现问题,所以不推荐)

public function getTree($list, $parent_id, $level=0) {
    //应该是静态的局部变量,这样才能保证,在递归调用时,所有
    //的getTree方法,操作的是一个Tree空间。
    static $tree = array();//保存找到的分类的数组
    //遍历所有分类,通过parent_id判断,哪些是我们正在查找的
    foreach($list as $row) {
        //判断当前所遍历的分类$row, 是否是当前需要查找的子分类
        if($row['pid'] == $parent_id) {
            //找到了一个分类
            //存起来,存哪?
            $row['level'] = $level;
            $tree[] = $row;
            //继续查找当前$row所代表的分类的子分类
            $this->getTree($list, $row['id'], $level+1);
        }
    }
    return $tree;
}
注意:
1、$list 已经asc排序过的所有数据