首页 > 代码库 > php N 维数组的读取、设置、删除

php N 维数组的读取、设置、删除

 

<?php
// 例子
$rowList = array();
$rowList[] = array(‘A‘=>‘A_1‘,‘B‘=>‘A_1_1‘,‘C‘=>‘A_1_1_1‘,‘D‘=>‘dira/dirb/dirc/node1‘,‘E‘=>‘nodeName1‘);
$rowList[] = array(‘A‘=>‘A_1‘,‘B‘=>‘A_1_1‘,‘C‘=>‘A_1_1_1‘,‘D‘=>‘dira/dirb/dirc/node2‘,‘E‘=>‘nodeName2‘);
$rowList[] = array(‘A‘=>‘A_1‘,‘B‘=>‘A_1_1‘,‘C‘=>‘A_1_1_2‘,‘D‘=>‘dira/dirb/dirc/node3‘,‘E‘=>‘nodeName3‘);
$rowList[] = array(‘A‘=>‘A_1‘,‘B‘=>‘A_1_1‘,‘C‘=>‘A_1_1_2‘,‘D‘=>‘dira/dirb/dirc/node4‘,‘E‘=>‘nodeName4‘);

$rowList[] = array(‘A‘=>‘A_1‘,‘B‘=>‘A_1_2‘,‘C‘=>‘A_1_2_1‘,‘D‘=>‘dira/dirb/dirc/node5‘,‘E‘=>‘nodeName5‘);
$rowList[] = array(‘A‘=>‘A_1‘,‘B‘=>‘A_1_2‘,‘C‘=>‘A_1_2_1‘,‘D‘=>‘dira/dirb/dirc/node6‘,‘E‘=>‘nodeName6‘);
$rowList[] = array(‘A‘=>‘A_1‘,‘B‘=>‘A_1_2‘,‘C‘=>‘A_1_2_2‘,‘D‘=>‘dira/dirb/dirc/node7‘,‘E‘=>‘nodeName7‘);
$rowList[] = array(‘A‘=>‘A_1‘,‘B‘=>‘A_1_2‘,‘C‘=>‘A_1_2_2‘,‘D‘=>‘dira/dirb/dirc/node8‘,‘E‘=>‘nodeName8‘);

$rowList[] = array(‘A‘=>‘A_2‘,‘B‘=>‘A_2_1‘,‘C‘=>‘A_2_1_1‘,‘D‘=>‘dira/dirb/dirc/node9‘,‘E‘=>‘nodeName9‘);
$rowList[] = array(‘A‘=>‘A_2‘,‘B‘=>‘A_2_1‘,‘C‘=>‘A_2_1_1‘,‘D‘=>‘dira/dirb/dirc/node10‘,‘E‘=>‘nodeName10‘);
$rowList[] = array(‘A‘=>‘A_2‘,‘B‘=>‘A_2_1‘,‘C‘=>‘A_2_1_2‘,‘D‘=>‘dira/dirb/dirc/node11‘,‘E‘=>‘nodeName11‘);
$rowList[] = array(‘A‘=>‘A_2‘,‘B‘=>‘A_2_1‘,‘C‘=>‘A_2_1_2‘,‘D‘=>‘dira/dirb/dirc/node12‘,‘E‘=>‘nodeName12‘);

$rowList[] = array(‘A‘=>‘A_2‘,‘B‘=>‘A_2_2‘,‘C‘=>‘A_2_2_1‘,‘D‘=>‘dira/dirb/dirc/node13‘,‘E‘=>‘nodeName13‘);
$rowList[] = array(‘A‘=>‘A_2‘,‘B‘=>‘A_2_2‘,‘C‘=>‘A_2_2_1‘,‘D‘=>‘dira/dirb/dirc/node14‘,‘E‘=>‘nodeName14‘);
$rowList[] = array(‘A‘=>‘A_2‘,‘B‘=>‘A_2_2‘,‘C‘=>‘A_2_2_2‘,‘D‘=>‘dira/dirb/dirc/node15‘,‘E‘=>‘nodeName15‘);
$rowList[] = array(‘A‘=>‘A_2‘,‘B‘=>‘A_2_2‘,‘C‘=>‘A_2_2_2‘,‘D‘=>‘dira/dirb/dirc/node16‘,‘E‘=>‘nodeName16‘);

// 填值
$nodeTree = array();
foreach ($rowList as $key => $value){
    $nodePath = array_pop($value);
    $nodeName = array_pop($value);
    $path = implode(‘/childs/‘, $value);
    $nodeInfo = array(
        ‘nodeName‘=>$nodeName,
        ‘nodePath‘=>$nodePath,
    );
    PathValueHelper::appendByPath($nodeTree, $path, $nodeInfo);
}
// print_r($nodeTree); // 打印存储结构

// 取值
$path = ‘A_1/childs/A_1_2/childs/A_1_2_1‘;
if(PathValueHelper::issetByPath($nodeTree, $path)){
    echo $path." is exist. \n";
    print_r(PathValueHelper::getByPath($nodeTree, $path));
}
else{
    echo $path." is not exist. \n";
}

$path = ‘A_1/childs/A_1_2/childs/A_1_2_5‘;
if(PathValueHelper::issetByPath($nodeTree,$path)){
    echo $path.‘ is exist.‘;
    print_r(PathValueHelper::getByPath($nodeTree,$path));
}
else{
    echo $path." is not exist. \n";
}

// 删除值
$path = ‘A_1/childs/A_1_2/childs/A_1_2_1‘;
if(PathValueHelper::issetByPath($nodeTree, $path)){
    echo $path." is unseted. \n";
    PathValueHelper::unsetByPath($nodeTree, $path);
}
else{
    echo $path." is not exist. \n";
}
$path = ‘A_1/childs/A_1_2/childs/A_1_2_1‘;
if(PathValueHelper::issetByPath($nodeTree, $path)){
    print_r(PathValueHelper::getByPath($nodeTree, $path));
}
else{
    echo $path." is not exist. \n";
}

print_r($nodeTree); // 打印存储结构

class PathValueHelper {

    /**
     * 追加值
     * @param unknown $data 容器变量
     * @param unknown $path 路径
     * @param unknown $value 值
     * @param string $delimiter 路径分隔符
     * @return boolean
     */
    public static function appendByPath(&$data,$path,$value,$delimiter=‘/‘){
        $pathSplit = explode($delimiter, $path);
        $dataTemp = &$data;
        foreach ($pathSplit as $keyName){
            if(isset($dataTemp[$keyName])){
                $dataTemp = &$dataTemp[$keyName];
                continue;
            }
            else{
                $dataTemp[$keyName] = array();
                $dataTemp = &$dataTemp[$keyName];
            }
        }
        $dataTemp[] = $value;
        return true;
    }

    /**
     * 设置值
     * @param unknown $data 容器变量
     * @param unknown $path 路径
     * @param unknown $value 值
     * @param string $delimiter 路径分隔符
     * @return boolean
     */
    public static function setByPath(&$data,$path,$value,$delimiter=‘/‘){
        $pathSplit = explode($delimiter, $path);
        $dataTemp = &$data;
        foreach ($pathSplit as $keyName){
            if(isset($dataTemp[$keyName])){
                $dataTemp = &$dataTemp[$keyName];
                continue;
            }
            else{
                $dataTemp[$keyName] = array();
                $dataTemp = &$dataTemp[$keyName];
            }
        }
        $dataTemp = $value;
        return true;
    }

    /**
     * 获取值
     * @param unknown $data 容器变量
     * @param unknown $path 路径
     * @param string $delimiter 路径分隔符
     * @return NULL|unknown
     */
    public static function getByPath(&$data,$path,$delimiter=‘/‘){
        $pathSplit = explode($delimiter, $path);
        $dataTemp = $data;
        foreach ($pathSplit as $keyName){
            if(isset($dataTemp[$keyName])){
                $dataTemp = $dataTemp[$keyName];
                continue;
            }
            else{
                return null;
            }
        }
        return $dataTemp;
    }

    /**
     * 是否设置值
     * @param unknown $data 容器变量
     * @param unknown $path 路径
     * @param string $delimiter 路径分隔符
     * @return boolean
     */
    public static function issetByPath($data,$path,$delimiter=‘/‘){
        $pathSplit = explode($delimiter, $path);
        $dataTemp = $data;
        foreach ($pathSplit as $keyName){
            if(isset($dataTemp[$keyName])){
                $dataTemp = $dataTemp[$keyName];
                continue;
            }
            else{
                return false;
            }
        }
        return true;
    }
    
    /**
     * 删除指定键的值
     * @param unknown $data
     * @param unknown $path
     * @param string $delimiter
     * @return boolean
     */
    public static function unsetByPath(&$data,$path,$delimiter=‘/‘){
        $pathSplit = explode($delimiter, $path);
        $dataTemp = &$data;
        $lastData = &$data;
        $lastKey = ‘‘;
        foreach ($pathSplit as $keyName){
            if(isset($dataTemp[$keyName])){
                $lastData = &$dataTemp;
                $lastKey = $keyName;
                $dataTemp = &$dataTemp[$keyName];
                continue;
            }
            else{
                return false;
            }
        }
        unset($lastData[$lastKey]);
        return true;
    }
}

 

php N 维数组的读取、设置、删除