Eagle-PsyX-
Commander
- Registriert
- Juni 2006
- Beiträge
- 2.247
Irgendwie stehe ich auf der Matte, vielleicht kann mir jemand ja helfen.
Mein Problem ist, ich kreige eine foreach()-Schleife nicht gebrochen. Mit break 2; erhalte ich eine Fehlermeldung.
Folgendes Array $category['tree']:
Und will nach EINER category_id suchen und diese inklusive ihrer Kinder zurückgeben.
Als Funktion nutze ich
Interessanterweise funktioniert das wunderbar, NUR nicht mit der Id 2.
Setze ich jedoch ein Break rein (das jetzt als Kommentar drine steht) klappt es. Ansonsten geht er die Rekursion 2x durch und überschreibt mir $return mit FALSE.
Mit var_dump($return) (siehe Kommentar) erhalte ich:
D.h. er tut einmal die ID zuordnen können, geht dann aber weiter anstatt abzubrechen.
Mein Problem ist, ich kreige eine foreach()-Schleife nicht gebrochen. Mit break 2; erhalte ich eine Fehlermeldung.
Folgendes Array $category['tree']:
Code:
Array
(
[0] => Array
(
[category_id] => 1
[cat_title] => Home
[category_left] => 1
[category_right] => 4
[category_visibility] => 1
[depth] => 0
[count_content] => 3
[count_gallery] => 0
[count_product] => 0
[children] => Array
(
[0] => Array
(
[category_id] => 2
[cat_title] => Über Uns
[category_left] => 2
[category_right] => 3
[category_visibility] => 1
[depth] => 3
[count_content] => 1
[count_gallery] => 2
[count_product] => 0
[children] => Array
(
)
)
)
)
[1] => Array
(
[category_id] => 3
[cat_title] => Automaten
[category_left] => 5
[category_right] => 12
[category_visibility] => 1
[depth] => 0
[count_content] => 0
[count_gallery] => 0
[count_product] => 0
[children] => Array
(
[0] => Array
(
[category_id] => 8
[cat_title] => blubb
[category_left] => 6
[category_right] => 9
[category_visibility] => 1
[depth] => 3
[count_content] => 0
[count_gallery] => 0
[count_product] => 0
[children] => Array
(
[0] => Array
(
[category_id] => 6
[cat_title] => Kaltgetränke
[category_left] => 7
[category_right] => 8
[category_visibility] => 1
[depth] => 8
[count_content] => 0
[count_gallery] => 0
[count_product] => 2
[children] => Array
(
)
)
)
)
[1] => Array
(
[category_id] => 5
[cat_title] => Snackautomaten
[category_left] => 10
[category_right] => 11
[category_visibility] => 1
[depth] => 3
[count_content] => 0
[count_gallery] => 0
[count_product] => 0
[children] => Array
(
)
)
)
)
)
Als Funktion nutze ich
PHP:
function category_find_id($id,$tree=0,$count=0) {
if($tree == 0) {
global $category;
$tree = $category['tree'];
}
$return = FALSE;
if($count <= 1000) {
foreach($tree AS $leaf) {
if($leaf['category_id'] == $id) {
$return = $leaf;
unset($tree);
break 1;
} elseif(count($leaf['children']) > 0) {
$return = category_find_id($id,$leaf['children'],($count+1));
}
#break;
}
} else { die('Ups..'); }
#var_dump($return);
return $return;
}
Setze ich jedoch ein Break rein (das jetzt als Kommentar drine steht) klappt es. Ansonsten geht er die Rekursion 2x durch und überschreibt mir $return mit FALSE.
Mit var_dump($return) (siehe Kommentar) erhalte ich:
Code:
array(10) {
["category_id"]=>
string(1) "2"
["cat_title"]=>
string(9) "Über Uns"
["category_left"]=>
string(1) "2"
["category_right"]=>
string(1) "3"
["category_visibility"]=>
string(1) "1"
["depth"]=>
string(1) "3"
["count_content"]=>
string(1) "1"
["count_gallery"]=>
string(1) "2"
["count_product"]=>
string(1) "0"
["children"]=>
array(0) {
}
}
bool(false)
bool(false)
bool(false)
Zuletzt bearbeitet: