PHP数组奇怪的行为

时间:2014-12-02 17:11:31

标签: php arrays json

我正在使用一个PHP类,它从MySQL数据库生成json格式的嵌套类别树菜单。问题是用于填充数据的数组没有按照预期的那样进行

PHP类是:

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; 
}

$ json应该看起来像:

[
    {"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}
            ]}
        ]}
    ]}
] 

但是,当我试图从MySQL中提取数据时,$ json数组只给我最后一次从MySQL提取数据,好像它正在删除所有数据

有人能跟我说话吗?

3 个答案:

答案 0 :(得分:2)

您正在使用categories周围的每个循环覆盖数组,并且每次都使用数组赋值擦除成员变量。

另外,我怀疑它不会返回你正在寻找的结构,除非你把它预先递归(这意味着返回你已经构建的阵列):

class nestedCategories {

    public function generateMenu($categories,$level){

        $categoriesJson = array();

        foreach($categories as $category ){

            $categoryJson = array();
            $categoryJson['title']    = $category->description;
            $categoryJson['expanded'] = true;

            if(isset($category->children) && count($category->children)>0){
                $categoryJson['folder'] = true;
                $categoryJson['children'] = $this->generateMenu($category->children,$category->category_id);
            } else {
                $categoryJson['folder'] = false;           
            }
            $categoriesJson[] = $categoryJson;
        }

        return $categoriesJson;
    }
}

现在,您只需通过调用generateMenu而不是使用成员变量来获取数据。

答案 1 :(得分:1)

您在$this->json = array();方法的顶部设置了generateMenu。当你进行递归调用时,它会破坏前一次调用中写入数组的内容。

此外,在每次递归调用中,$this->json['title']将覆盖以前的标题。 expandedchildrenfolder也是如此。

看起来我花了一点时间才能制作示例代码。无论如何它在这里。

class Foo {
    public function generateMenu($categories){
        $json_array = array();
        foreach($categories as $category) {
            $json_object = array();
            $json_object['title'] = $category->description;

            if(isset($category->children) && count($category->children)>0){
                // it looks like expanded should only be 
                // in objects with > 0 children
                $json_object['expanded'] = true;
                $json_object['folder'] = true;
                $json_object['children'] = $this->generateMenu($category->children);

            } else {
                // based on the json in the question it looks like
                // it should always contain a children array, but no folder element
                $json_object['children'] = array();           
            }
            $json_array[] = $json_object;
        }
        return $json_array;
    }
}

以下是测试数据:

$Diptera = new stdClass();
$Diptera->description = 'Diptera';

$Insect = new stdClass();
$Insect->description = 'Insect';
$Insect->children = array($Diptera);

$Arthropoda = new stdClass();
$Arthropoda->description = 'Arthropoda';
$Arthropoda->children = array($Insect);

$Felidae = new stdClass();
$Felidae->description = 'Felidae';

$Carnivora2 = new stdClass();
$Carnivora2->description = 'Carnivora';
$Carnivora2->children = array($Felidae);

$Carnivora = new stdClass();
$Carnivora->description = 'Carnivora';

$Primate2 = new stdClass();
$Primate2->description = 'Primate';

$Primate = new stdClass();
$Primate->description = 'Primate';
$Primate->children = array($Primate2, $Carnivora);

$Mammal = new stdClass();
$Mammal->description = 'Mammal';
$Mammal->children = array($Primate, $Carnivora2);

$Chordate = new stdClass();
$Chordate->description = 'Chordate';
$Chordate->children = array($Mammal);

$Animalia = new stdClass();
$Animalia->description = 'Animalia';
$Animalia->children = array($Chordate);

$catagories = array($Animalia);

以下是它的名称:

$f = new Foo();
echo json_encode($f->generateMenu($catagories), JSON_PRETTY_PRINT);

这是输出:

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

答案 2 :(得分:0)

迭代JSON时,您将覆盖数组键。

$array = array();
$array['foo'] = 'bar';
//$array['foo] = 'bar';

$array['foo'] = 'foo';
//$array['foo'] = 'foo';