mardi 2 décembre 2014

PHP Array strange behavior


Vote count:

0




I am using a PHP Class that is generating a nested categories tree menu in json format from a MySQL database. The problem is that the array used to fill the data doesn't do what it is supposed to


The PHP Class is:



class nestedCategories {
public $json = array();

public function generateMenu($categories,$level){
$this->json = array();
foreach($categories as $category ){
$this->json['title'] = $category->description;
$this->json['expanded'] = true;

if(isset($category->children) && count($category->children)>0){
$this->json['folder'] = true;
$this->json['children'] = $this->generateMenu($category->children,$category->category_id);
} else {
$this->json['folder'] = false;
}
}
}
public function getJSON() {
return $this->json;
}


The $json is supposed to look like:



[
{"title": "Animalia", "expanded": true, "folder": true, "children": [
{"title": "Chordate", "folder": true, "children": [
{"title": "Mammal", "children": [
{"title": "Primate", "children": [
{"title": "Primate", "children": [
]},
{"title": "Carnivora", "children": [
]}
]},
{"title": "Carnivora", "children": [
{"title": "Felidae", "lazy": true}
]}
]}
]},
{"title": "Arthropoda", "expanded": true, "folder": true, "children": [
{"title": "Insect", "children": [
{"title": "Diptera", "lazy": true}
]}
]}
]}
]


but, when I am trying to pull the data from MySQL, the $json array only gives me the last pulled data from MySQL, as if it is erasing all data behind


Could someone talk me trough?



asked 1 min ago







PHP Array strange behavior

Aucun commentaire:

Enregistrer un commentaire