我有一个叫做Member的类。我试图添加一个多维数组作为一个属性,如:
class Member {
public $name;
public $inheritingMembers;
public function __construct($name, $family) {
$this->name = $name;
$this->inheritingMembers = $this->findInheritingMembers($family, $name);
}
function findInheritingMembers($array, $keySearch) {
foreach ($array as $key => $item){
if ($key == $keySearch){
return $item['children'];
} else {
if (is_array($item))
$this->findInheritingMembers($item, $keySearch);
}
}
return false;
}
}
函数"findinheritingmembers"返回一个多维数组,我可以看到与var_dump()。问题是,我似乎不能分配这个数组作为成员对象的继承成员属性。
我想你的递归函数需要另一个返回:
function findInheritingMembers($array, $keySearch) {
foreach ($array as $key => $item){
if ($key == $keySearch){
return $item['children'];
} else {
if (is_array($item))
return $this->findInheritingMembers($item, $keySearch);
}
}
return false;
}