Server : nginx/1.20.1
System : Linux iZ2ze9ojcl78uluczwag69Z 4.18.0-240.22.1.el8_3.x86_64 #1 SMP Thu Apr 8 19:01:30 UTC 2021 x86_64
User : www ( 1000)
PHP Version : 7.3.28
Disable Function : passthru,exec,system,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv
Directory :  /www/wwwroot/0531yanglao.com/extend/phpTools/
Upload File :
Current Directory [ Writeable ] Root Directory [ Writeable ]


Current File : /www/wwwroot/0531yanglao.com/extend/phpTools/ArrayTools.php
<?php
namespace phpTools;

header('content-type:text/html;charset=utf-8');
/**
 * 数组工具类
 * Class ArrayTools
 */

class ArrayTools{


    /**
     * 从二维数组中取出自己要的KEY值
     * @param  array $arrData
     * @param string $key
     * @param $im true 返回逗号分隔
     * @return array
     */
    public static function filter_value($arrData, $key, $im = false)
    {
        $re = [];
        foreach ($arrData as $k => $v) {
            if (isset($v[$key])) $re[] = $v[$key];
        }
        if (!empty($re)) {
            $re = array_flip(array_flip($re));
            sort($re);
        }
    
        return $im ? implode(',', $re) : $re;
    }
    
    /**
     * 重设键,转为array(key=>array())
     * @param array $arr
     * @param string $key
     * @return array
     */
    public static function reset_by_key($arr, $key)
    {
        $re = [];
        foreach ($arr as $v) {
            $re[$v[$key]] = $v;
        }
    
        return $re;
    }
    
    /**
     * 节点遍历
     *
     * @param        $list
     * @param string $pk
     * @param string $pid
     * @param string $child
     * @param int    $root
     *
     * @return array
     */
    public static function list_to_tree($list, $pk = 'id', $pid = 'parent_id', $child = '_child', $root = 0)
    {
        // 创建Tree
        $tree = [];
        if (is_array($list)) {
            // 创建基于主键的数组引用
            $refer = [];
            foreach ($list as $key => $data) {
                if ($data instanceof \think\Model) {
                    $list[$key] = $data->toArray();
                }
                $refer[$data[$pk]] =& $list[$key];
            }
            foreach ($list as $key => $data) {
                // 判断是否存在parent
                if (!isset($list[$key][$child])) {
                    $list[$key][$child] = [];
                }
                $parentId = $data[$pid];
                if ($root == $parentId) {
                    $tree[] =& $list[$key];
                } else {
                    if (isset($refer[$parentId])) {
                        $parent =& $refer[$parentId];
                        $parent[$child][] =& $list[$key];
                    }
                }
            }
        }
    
        return $tree;
    }
    

    /**
     * 从数组中删除空白的元素(包括只有空白字符的元素)
     *
     * @param array $arr
     * @param boolean $trim
     */
    public function array_remove_empty(& $arr, $trim = true)
    {
        foreach ($arr as $key => $value) {
            if (is_array($value)) {
                self::array_remove_empty($arr[$key]);
            } else {
                $value = trim($value);
                if ($value == '') {
                    unset($arr[$key]);
                } elseif ($trim) {
                    $arr[$key] = $value;
                }
            }
        }
    }
    
    /**
     * 从一个二维数组中返回指定键的所有值
     *
     * @param array $arr
     * @param string $col
     *
     * @return array
     */
    public static function array_col_values(& $arr, $col)
    {
        $ret = array();
        foreach ($arr as $row) {
            if (isset($row[$col])) {
                $ret[] = $row[$col];
            }
        }
        return $ret;
    }
    
    /**
     * 将一个二维数组转换为 hashmap
     *
     * 如果省略 $valueField 参数,则转换结果每一项为包含该项所有数据的数组。
     *
     * @param array $arr
     * @param string $keyField
     * @param string $valueField
     *
     * @return array
     */
    public static function array_to_hashmap(& $arr, $keyField, $valueField = null)
    {
        $ret = array();
        if ($valueField) {
            foreach ($arr as $row) {
                $ret[$row[$keyField]] = $row[$valueField];
            }
        } else {
            foreach ($arr as $row) {
                $ret[$row[$keyField]] = $row;
            }
        }
        return $ret;
    }
    
    /**
     * 将一个二维数组按照指定字段的值分组
     *
     * @param array $arr
     * @param string $keyField
     *
     * @return array
     */
    public static function array_group_by(& $arr, $keyField)
    {
        $ret = array();
        foreach ($arr as $row) {
            $key = $row[$keyField];
            $ret[$key][] = $row;
        }
        return $ret;
    }
    
    /**
     * 将一个平面的二维数组按照指定的字段转换为树状结构
     *
     * 当 $returnReferences 参数为 true 时,返回结果的 tree 字段为树,refs 字段则为节点引用。
     * 利用返回的节点引用,可以很方便的获取包含以任意节点为根的子树。
     *
     * @param array $arr
     *            原始数据
     * @param string $fid
     *            节点ID字段名
     * @param string $fparent
     *            节点父ID字段名
     * @param string $fchildrens
     *            保存子节点的字段名
     * @param boolean $returnReferences
     *            是否在返回结果中包含节点引用
     *
     *            return array
     */
    public static function array_to_tree($arr, $fid, $fparent = 'parent_id', $fchildrens = '_child', $returnReferences = false) // childrens
    {
        $pkvRefs = array();
        foreach ($arr as $offset => $row) {
            $pkvRefs[$row[$fid]] = & $arr[$offset];
        }
    
        $tree = array();
        foreach ($arr as $offset => $row) {
            $parentId = $row[$fparent];
            if ($parentId) {
                if (! isset($pkvRefs[$parentId])) {
                    continue;
                }
                $parent = & $pkvRefs[$parentId];
                $parent[$fchildrens][] = & $arr[$offset];
            } else {
                $tree[] = & $arr[$offset];
            }
        }
        if ($returnReferences) {
            return array(
                'tree' => $tree,
                'refs' => $pkvRefs
            );
        } else {
            return $tree;
        }
    }
    
    /**
     * 将树转换为平面的数组
     *
     * @param array $node
     * @param string $fchildrens
     *
     * @return array
     */
    public static function tree_to_array(& $node, $fchildrens = 'childrens')
    {
        $ret = array();
        if (isset($node[$fchildrens]) && is_array($node[$fchildrens])) {
            foreach ($node[$fchildrens] as $child) {
                $ret = array_merge($ret, tree_to_array($child, $fchildrens));
            }
            unset($node[$fchildrens]);
            $ret[] = $node;
        } else {
            $ret[] = $node;
        }
        return $ret;
    }
    
    /**
     * 根据指定的键值对数组排序
     *
     * @param array $array
     *            要排序的数组
     * @param string $keyname
     *            键值名称
     * @param int $sortDirection
     *            排序方向
     *
     * @return array
     */
    public static function array_column_sort($array, $keyname, $sortDirection = SORT_ASC)
    {
        return self::array_sortby_multifields($array, array(
            $keyname => $sortDirection
        ));
    }
    
    /**
     * 将一个二维数组按照指定列进行排序,类似 SQL 语句中的 ORDER BY
     *
     * @param array $rowset
     * @param array $args
     */
    public static function array_sortby_multifields($rowset, $args)
    {
        $sortArray = array();
        $sortRule = '';
        foreach ($args as $sortField => $sortDir) {
            foreach ($rowset as $offset => $row) {
                $sortArray[$sortField][$offset] = $row[$sortField];
            }
            $sortRule .= '$sortArray[\'' . $sortField . '\'], ' . $sortDir . ', ';
        }
        if (empty($sortArray) || empty($sortRule)) {
            return $rowset;
        }
        eval('array_multisort(' . $sortRule . '$rowset);');
        return $rowset;
    }
    
    public static function array_to_array2($data=[]) 
    {
        if(empty($data)) return ;
        $res=[];
        foreach ($data as $key=>$value) {
            $res[]=['key'=>$key,'value'=>$value];
        }
        return $res;
    }
    
    /**
     * XML编码
     * @param mixed $data 数据
     * @param string $root 根节点名
     * @param string $item 数字索引的子节点名
     * @param string $id 数字索引子节点key转换的属性名
     * @return string
     */
    static public function arr2xml($data, $root = 'xml', $item = 'item', $id = 'id')
    {
        return "<{$root}>" . self::_data_to_xml($data, $item, $id) . "</{$root}>";
    }
    
    /**
     * XML内容生成
     * @param array $data 数据
     * @param string $item 子节点
     * @param string $id 节点ID
     * @param string $content 节点内容
     * @return string
     */
    static private function _data_to_xml($data, $item = 'item', $id = 'id', $content = '')
    {
        foreach ($data as $key => $val) {
            is_numeric($key) && $key = "{$item} {$id}=\"{$key}\"";
            $content .= "<{$key}>";
            if (is_array($val) || is_object($val)) {
                $content .= self::_data_to_xml($val);
            } elseif (is_numeric($val)) {
                $content .= $val;
            } else {
                $content .= '<![CDATA[' . preg_replace("/[\\x00-\\x08\\x0b-\\x0c\\x0e-\\x1f]/", '', $val) . ']]>';
            }
            list($_key,) = explode(' ', $key . ' ');
            $content .= "</$_key>";
        }
        return $content;
    }
    
    
    /**
     * 将xml转为array
     * @param string $xml
     * @return array
     */
    
    public static function xmlToArray($xml)
    {
        return json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
    }

    /**
     * 获取选中值
     * @param $attr 原数据
     * @param $value    选中值
     * @return array|bool
     */
    public static function getSelValue($attr, $value)
    {
        $data = [];
        if(!is_array($value)) {
            $value = explode(',', $value);
        }
        if(empty($value)) {
            return $data;
        }

        foreach($value as $v) {
           if(isset($attr[$v])) {
               $data[] = $attr[$v];
           }
        }

        return $data;
    }
    
}