1

好吧,我真的很坚持这个。我希望你能帮助我。

我有我的班级,用于管理分层数据。输入是一个具有以下结构的普通数组(只是一个示例):

$list = array(
(object) array('id' => 1, 'nombre' => 'Cámaras de fotos', 'parentId' => null),
(object) array('id' => 2, 'nombre' => 'Lentes', 'parentId' => null),
(object) array('id' => 3, 'nombre' => 'Zoom', 'parentId' => 2),
(object) array('id' => 4, 'nombre' => 'SLR', 'parentId' => 1),
(object) array('id' => 5, 'nombre' => 'Primarios', 'parentId' => 2),
(object) array('id' => 6, 'nombre' => 'Sensor APS-C', 'parentId' => 4),
(object) array('id' => 7, 'nombre' => 'Full-frame', 'parentId' => 4),
(object) array('id' => 8, 'nombre' => 'Flashes', 'parentId' => null),
(object) array('id' => 9, 'nombre' => 'Compactas', 'parentId' => 1)
);

我以这种方式将数据输入到类中:

$Hierarchical = new Hierarchical;
$Hierarchical->plain = $list;

然后我有一个公共函数 ( createTree) 来创建列表的多维数组表示。它完美地工作。它可以返回结果或将其存储在$this->tree.

如您所见,这非常简单。它调用私有函数iterateTree,即递归函数。

class Hierarchical {

    public $plain = array();
    public $tree = array();

    public function createTree($parentId=0, $return=false) {

    $tree = $this->iterateTree($parentId);

    if(!$return) {
        $this->tree = $tree;
    } else {
        return $tree;
    }

    }


    private function iterateTree($parentId) {

    $resArray = array();

    foreach($this->plain as $item) {

        if($item->parentId == $parentId) {

            $children = $this->iterateTree($item->id);

            if( count($children) > 0 ) {
                $item->children = $children;
            }

            $resArray[] = $item;

        }

    }

    return $resArray;

    } 

}

到目前为止,一切都很好。它工作正常。

但是......当我想$this->plain在调用后使用时出现问题createTree()。它不是返回原始数据集,而是返回原始输入之间的某种混合,并附加所有子数据集(类似于$this->tree)。

我无法弄清楚为什么$this->plain要更改的内容,在使用的两个函数中我都没有更改它的内容。

我尝试在foreach, 之后取消设置变量foreach,甚至将原始数组作为参数传递,并且$this->plain在递归函数中根本不使用。没有任何效果。

我也没有在类中使用任何其他可能改变它的值的函数。

这完全是个谜!

4

3 回答 3

1

根据道格的回答,正确的功能是:(添加$itemAux = clone $item

private function iterateTree($parentId) {

    $resArray = array();

    foreach($this->plain as $item) {

        $itemAux = clone $item;

        if($itemAux->parentId == $parentId) {

            $children = $this->iterateTree($itemAux->id);

            if( count($children) > 0 ) {
                $itemAux->children = $children;
            }

            $resArray[] = $itemAux;

        }

    }

    return $resArray;

} 
于 2012-05-24T22:56:58.160 回答
1

在您的foreach循环$item中将引用数组中的对象,因此您正在更改该行中的同一对象

$item->children = $children;

这将影响原始数组中引用的对象$list$this->plain.

一种解决方案可能是$item在您的 foreach 循环中进行克隆。

于 2012-05-23T23:29:20.360 回答
0

为了补充道格的答案,虽然手册说“对象不是通过引用传递”(http://www.php.net/manual/en/language.oop5.references.php),但它可能有助于考虑对象作为一个完全独立的实体与任何可能“包含”它们的变量分开,并且它们实际上通过引用传递到任何地方......

class testClass
{
    public $var1 = 1;
}

function testFunc($obj)
{
    $obj->var1 = 2;
}


$t = new testClass;
testFunc($t);
echo $t->var1; // 2

因此,当您这样做时$item->children = $children;,您实际上是在影响该$plain数组中的每个原始对象。

于 2012-05-24T00:02:34.180 回答