分类: PHP
2014-03-21 18:11:39
缺点:相同数据格式的数值无法叠加,页面数据格式太繁琐,最初下标值为整数的结构会出错假设基础数据字段列表baseRowList格式是 array(array('pid'=>xxxx, 'id'=>xxxx, 'name'=>xxx, 'age'=>xxx), ....)页面数据显示dataStruct格式是array('list'=>array('pid'=> array('id' => array('name', 'age'))),'nameList' => array('id'=>'name')),1. 将dataStruct数组 递归抽取转化为可显示层级结构的字符串如将dataStruct转化为structList = array("list,pid,id,name", "list,pid,id,age", "nameList,id,name"), getStructList函数2. 读取一条基础数据,循环分隔structList,将其中的字串关键字替换为基础数值或者不变3. 重复循环2直至基础数据所有处理完成代码如下:private function getsRowStructData($baseRowList, $dataStruct){$list = array();if($baseRowList){$structList = self::getStructList($dataStruct);$rowKeyArr = self::getRowKeyArr($baseRowList);foreach ($baseRowList as $row){if($structList)foreach ($structList as $struct){$list = $list + self::getOneStructData(explode(',', $struct), $row, $rowKeyArr);}}}return $list;}// 获取 列表关键字下标数组private function getRowKeyArr($list){$arr = NULL;if($list)foreach ($list as $row)if(is_array($row))foreach ($row as $k => $v)$arr[] = $k;return $arr;}// 将层级结构数组中的关键字 替换为实际值,并返回 页面结构数据private function getOneStructData($oneStructList, $dataRow, $rowKeyArr){$row = array();if($oneStructList){for($i=count($oneStructList)-1; $i>=0; $i--){if(in_array($oneStructList[$i], $rowKeyArr)){$row = ($row) ? array($dataRow[$oneStructList[$i]]=>$row) : $dataRow[$oneStructList[$i]] ;}else{$row = ($row) ? array($oneStructList[$i]=>$row) : $oneStructList[$i];}}}return $row;}// 根据页面显示的数据结构返回 层级结构数组private function getStructList($struct, $list=array(), $prefix=NULL){$tmpList = $list;if(is_array($struct)){foreach ($struct as $k => $v){if(is_array($v)){$tmpPrefix = ($prefix) ? "{$prefix},{$k}": $k;$tmpList = self::getStructList($v, $tmpList, $tmpPrefix);}else{$tmpPrefix = ($prefix) ? "{$prefix},{$v}": $v;array_push($tmpList, $tmpPrefix);}}return $tmpList;}elseif($struct){array_push($tmpList, ($prefix) ? "{$prefix},{$struct}": $struct);return self::getStructList(NULL, $tmpList, $prefix);}elsereturn $tmpList;}
缺点:相同数据格式的数值无法叠加,页面数据格式太繁琐假设基础数据字段列表baseRowList格式是 array(array('pid'=>xxxx, 'id'=>xxxx, 'name'=>xxx, 'age'=>xxx), ....)页面数据显示dataStruct格式是array('list'=>array('pid'=> array('id' => array('name', 'age'))),'nameList' => array('id'=>'name')),1. 读取一条基础数据,循环dataStruct,将其中的字串关键字替换为基础数值或者不变2. 重复循环1直至基础数据所有处理完成static function getRowStructData2($baseRowList, $dataStruct){
$list = array();if($baseRowList){
$rowKeyArr = self::getRowKeyArr($baseRowList);foreach ($baseRowList as $row){$list = array_merge_recursive($list, self::getOneRowStructData2($dataStruct, $row, $rowKeyArr));}
}return $list;
}private function getOneRowStructData2($dataStruct, $dataRow, $rowKeyArr, $prefix='', $list=array()){
if($dataStruct && count($dataStruct)){
foreach ($dataStruct as $k => $v){
if(is_array($v)){if(in_array($k, $rowKeyArr))$list = array_merge_recursive($list, self::getOneRowStructData2($v, $dataRow, $rowKeyArr, $prefix."[$dataRow[$k]]"));else$list = array_merge_recursive($list, self::getOneRowStructData2($v, $dataRow, $rowKeyArr, $prefix."[$k]"));}else{if(in_array($v, $rowKeyArr))eval("\$list$prefix = $dataRow[$v];");else
eval("\$list$prefix = $v;");}
}
}return $list;
}
缺点:相同数据格式的数值无法叠加假设基础数据字段列表baseRowList格式是 array(array('pid'=>xxxx, 'id'=>xxxx, 'name'=>xxx, 'age'=>xxx), ....)页面数据显示dataStruct格式是array('list,pid,id,name,age','nameList,id,name'),1. 读取一条基础数据,循环dataStruct,处理成层级结构,并将其中的字串关键字替换为基础数值或者不变2. 重复循环1直至基础数据所有处理完成static function getRowStructData3($baseRowList, $dataStruct){
$list = array();if($baseRowList){
$rowKeyArr = self::getRowKeyArr($baseRowList);foreach ($baseRowList as $row){
$list = array_merge_recursive($list, self::getsRowStructData3($dataStruct, $row, $rowKeyArr));
}
}return $list;
}
private function getsRowStructData3($dataStruct, $dataRow, $rowKeyArr){
$list=array();if($dataStruct && count($dataStruct)){
foreach ($dataStruct as $struct){
if($structArr = explode(',', $struct)){
$count = count($structArr);$prefix = '';for($i=0; $i<$count-1; $i++)$prefix .= (in_array($structArr[$i], $rowKeyArr)) ? "[{$dataRow[$structArr[$i]]}]" : "[{$structArr[$i]}]";$key = $structArr[$count-1];if(in_array($key, $rowKeyArr))eval(" \$list$prefix += $dataRow[$key];");elseeval(" \$list$prefix += $key;");
}
}
}return $list;
}
假设基础数据字段列表baseRowList格式是 array(array('pid'=>xxxx, 'id'=>xxxx, 'name'=>xxx, 'age'=>xxx), ....)页面数据显示dataStruct格式是array('list,pid,id,name,age','nameList,id,name'),1. 读取一条基础页面数据,依条将baseRowList处理成层级结构,将并将其中的字串关键字替换为基础数值或者不变2. 重复循环1直至页面接口数据处理完成static function getRowStructData4($baseRowList, $dataStruct){
$list = array();if($dataStruct && count($dataStruct) && $baseRowList && count($baseRowList)){
$rowKeyArr = self::getRowKeyArr($baseRowList);foreach ($dataStruct as $struct){
if($structArr = explode(',', $struct)){
$count = count($structArr);foreach ($baseRowList as $dataRow){
$prefix = '';for($i=0; $i<$count-1; $i++)$prefix .= (in_array($structArr[$i], $rowKeyArr)) ? "[{$dataRow[$structArr[$i]]}]" : "[{$structArr[$i]}]";$key = $structArr[$count-1];if(in_array($key, $rowKeyArr))eval(" \$list$prefix = is_numeric(\$list$prefix) ? \$list$prefix + $dataRow[$key] : $dataRow[$key]; ");elseeval(" \$list$prefix = is_numeric(\$list$prefix) ? \$list$prefix + $key : $key; ");
}
}
}
}return $list;
}