【发布时间】:2014-08-21 17:15:10
【问题描述】:
我有来自数据库的子变量。比如:
$roots = $this->getCategoryTable()->getCategoryRoot($project_id);//themes
if($roots->count()> 0){
foreach($roots as $root){
$root_id = $root->id;
//show name root
echo $root->name;
//show children of root
if($this->getCategoryTable()->checkHasRowsChildren($root_id)){
$rootChild = $this->getCategoryTable()->getRowsChildren($root_id);
if($rootChild->count() > 0){
foreach($rootChild as $key => $val) {
$array_themes[]=$val->name;
$result = $this->getChild($val->id, $array_themes);
}//end of foreach child of root
}//end of existing child of root
}
}//end of foreach root
}//end of existing root
这是我生孩子的功能
function getChild($root_id, $array_themes) {
if($this->getCategoryTable()->checkHasRowsChildren($root_id)){
$rootChild = $this->getCategoryTable()->getRowsChildren($root_id);
if($rootChild->count() > 0){
foreach($rootChild as $key => $val) {
$array_themes[]=$val->name;
$this->getChild($val->id, $array_themes);
}//end of foreach child of root
}//end of existing child of root
}
}
结果应该是这样的:
孩子:父母
H : G
G : D
J : C
F : C
C : A
乙:甲
答:E
E : D
D:空
我得到了所有的父母和孩子。但最终的结果是我想得到像
这样的结果数组 $array_result = array(
[0]=>"D",
[1]=>"E",
[2]=>"A",
[3]=>"C",
[4]=>"F",
[5]=>"J",
[6]=>"G",
[7]=>"H"
);
有可能得到这样的数组结果吗?
【问题讨论】:
-
请显示源(原始)数组的外观
-
$array_result = array( [0]=>"H" );
标签: php arrays function recursion multidimensional-array