hallo,
habe ein problem mit einer rekursiven function - möchte folgendes array durchlaufen und jeweils die kategorien zuordnen - also die pid ist immer die parent kategorie wo das element rein soll. das klappt auch bis zur 2ten ebene - nur danach ist schluss - also "sub kat 3" landet nicht da wo es soll:
habe ein problem mit einer rekursiven function - möchte folgendes array durchlaufen und jeweils die kategorien zuordnen - also die pid ist immer die parent kategorie wo das element rein soll. das klappt auch bis zur 2ten ebene - nur danach ist schluss - also "sub kat 3" landet nicht da wo es soll:
PHP-Code:
Array
(
[0] => Array
(
[cid] => 7
[pid] => 3
[titel] => Sub Kat 3
)
[1] => Array
(
[cid] => 3
[pid] => 1
[titel] => Sub Kat 1
)
[2] => Array
(
[cid] => 4
[pid] => 1
[titel] => Sub Kat 1
)
[3] => Array
(
[cid] => 1
[pid] => 0
[titel] => Kat 1
)
[4] => Array
(
[cid] => 2
[pid] => 0
[titel] => Kat 2
)
[5] => Array
(
[cid] => 5
[pid] => 0
[titel] => Kat 3
)
[6] => Array
(
[cid] => 6
[pid] => 0
[titel] => Kat 4
)
)
public function getData($id = null) {
if ($id != null)
$this->getDb()->where("pid = $id");
$this->getDb()->orderBy("prio,titel", "asc");
$res = $this->getDb()->get($this->getTable());
$data = [];
foreach ($res as $row) {
if ($row['pid'] == 0) {
$data[$row['cid']]['titel'] = $row['titel'];
} else {
$data[$row['pid']]['data'][$row['cid']]['titel'] = $row['titel'];
}
$subArr = $this->hasSub($row['cid']);
if (count($subArr) > 0) {
$this->getData($row['cid']);
}
}
return $data;
}
Kommentar