php array 使用笔记

这里总结了下最近处理php数组所写的函数

:::brush:php;toolbar:false
    //demo
    $demoArray = array(
            0=>array(
                    "a"=>1,
                    "append"=>2,
            ),
            1=>array(
                    "a"=>1,
                    "append"=>2,
            ),
            2=>array(
                    "a"=>1,
                    "append"=>2,
            ),
            3=>array(
                    "a"=>1,
                    "append"=>3,
            ),
    );
    $result = phpArray::selectArrayKeys($demoArray,"append",array("append","a"));
    print_r($result);
    exit;

/**
 *  php操作数组
 */

class phpArray{



    function __construct(){

    }

    

        /*
         *      数组按某个字段分组       
         */
        public static function arrayGroupBy($array=array(),$key="",$subKey=""){
                $newArray = array();
                foreach($array as $item){
                        if(isset($item[$key]) && !empty($subKey) && isset($item[$subKey])){
                                $newArray[$item[$key]][$item[$subKey]] = $item;         //指定二级数组索引.$item[$subKey]有相同值会被覆盖
                        }elseif(isset($item[$key])){
                                $newArray[$item[$key]][] = $item;
                        }else{
                                $newArray[] = $item;
                        }
                }       
                return $newArray;
        }


        /*
         *      设置数组索引
         */
        public static function setArrayKey($array=array(),$key=""){
                $newArray = array();
                foreach($array as $item){
                        $newArray[$item[$key]] = $item;         //有相同值会被覆盖
                }
                return $newArray;       
        }




        /*
         *      筛选数组字段,仍按行返回
         */
        public static function selectArrayKeys($array=array(),$key="",$fields=array(),$type=0){
                $newArray = array();
                foreach($array as $item){
                        $row = array();
                        foreach ($fields as $k) {
                                if(!isset($item[$k])) continue;
                                if(1==$type){                                   //只返回一个字段的值
                                        $row = $item[$k];
                                }else{
                                        $row[$k] = $item[$k];           //仍返回二级数组
                                }
                                $newArray[$item[$key]] = $row;
                        }
                }
                return $newArray;       
        }




        




        /*
         * 从数组中获取指定字段[获取行]
         */




        public static function arrayWhereIn($fields = array(), $data = array()){
                $fieldsData = array();
                foreach ($fields as $key){
                        if(isset($data[$key])){
                                $fieldsData[$key] = is_string($data[$key])?addslashes($data[$key]):$data[$key]; //转义字符串
                        }
                }


                return $fieldsData;
        }




        /*
         * 批量获取数组中的数据[获取某几列数据]
         */
        public static function getArrayColumns($data, $keys){
                $items = array();
                foreach ($data as $row){
                        foreach ($row as $field=>$val){
                                if(in_array($field, $keys)){
                                        $items[$field][] = $val;
                                }
                        }
                }


                return $items;


        }

        //获取某一列
        public static function getArrayColumn($data, $key){
            $column = A::getArrayColumns($data, array($key));
            $column = A::getArrayItem($column,$key,array());
            return $column;
        }

        /**
         * 获取数组中的值
         */
        function getArrayItem($data, $key, $default = 0){
            $itemValue = isset($data[$key])?$data[$key]:$default;
            return $itemValue;
        }


        /*
         *  二维数组行列转换
         */
        public static function array2DInverse($array = array()){
                $precent = array();
                foreach($array as $i=>$arr){
                        if(is_array($arr)){
                                foreach($arr as $j=>$v){
                            $precent[$j][$i]=$array[$i][$j];
                        }
                        }else{
                                $precent[0][$i]=$arr;
                        }
               
            }
            return $precent;
        }


        /*
         *  二维数组添加列
         */
        public static function arrayAddColumn($array = array(),$add=array(),$fieldName=""){
                foreach($array as $k=>$row){
                        $array[$k][$fieldName]  = isset($add[$k])?$add[$k]:"";          
                }       
                return $array;
        }






}

This article is my 7th oldest. It is 281 words long, and it’s got 0 comments for now.