/** * 多维数组转换为一维数组 * * @param $array 原数组 * @param null $child 子数组key值 * @param array $columnFix 要添加标志的colomn值 * @param null $level level层级 * e.g: multiArray2LinearArray($multiArray, null, ['category_name', '----']); * * @return array|mixed */ function multiArray2LinearArray(&$array, $child = null, array $columnFix = [null, null], $level = null) { global $result; is_null($child) && ($child = '_child'); $column = $columnFix[0]; $fix = $columnFix[1]; is_null($level) && ($level = 1); empty($result) && ($result = []); if (is_array($array) && count($array)) { foreach ($array as &$item) { if (!is_null($column) && !is_null($fix) && $item[$column]) { $item[$column] = '|' . str_repeat($fix, $level) . $item[$column]; } $result[] = $item; if (!empty($item[$child]) && count($item[$child])) { multiArray2LinearArray($item[$child], $child, $columnFix, ++$level); $level--; } } } return $result; }
/** * 获取类别的类别树的一维列表 * * @return array|mixed */ public function getListtree2Linear($columnFix = ['category_name', '----']) { $tree = $this->getListtree(); $linearArray = multiArray2LinearArray($tree, null, $columnFix); return $linearArray; }