首页 > 代码库 > 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 维数组的读取、设置、删除
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。