PHP引用的问题

PHP引用的问题,php,Php,我有以下PHP函数 protected function &__group($ar_path, $b_create) { assert('is_null($ar_path) || is_array($ar_path)'); $parent = &$this->ma_config; if (!is_null($ar_path) && (count($ar_path) > 0)) { if ($b_create)

我有以下PHP函数

protected function &__group($ar_path, $b_create) {
    assert('is_null($ar_path) || is_array($ar_path)');
    $parent = &$this->ma_config;

    if (!is_null($ar_path) && (count($ar_path) > 0)) {
        if ($b_create) {
            // Get with Create is necessary
            foreach ($ar_path as $group) {
                if (!array_key_exists('groups', $parent)) {
       /*MARKER*/   $parent['groups'] = array($group => array()); 
                    // Mark the File as Modified
                    $this->mb_dirty = true;
                    continue;
                }

                $parent = &$parent['groups'];
                if (!array_key_exists($group, $parent)) {
                    $parent[$group] = array();
                    // Mark the File as Modified
                    $this->mb_dirty = true;
                    continue;
                }

                $parent = &$parent[$group];
            }
        } else {
            // Simple Get
            foreach ($ar_path as $group) {
                $parent = array_extract_key(
                    array('groups', $group), 
                    $parent, 
                    'is_array'
                );
                if (is_null($parent)) {
                    break;
                }
            }
        }
    }
    return $parent;
}
该函数允许我在任何级别创建“组”(即嵌套组)

问题似乎在于PHP处理引用的方式。 当我发送类似于
ar_path('level 1','level 2')
的内容时,如果“level 1”不存在,函数应该创建一个子组

因此,假设:

$this->ma_config = array(
    'groups' => array(
        'level 1' => array(
            'values' => array(
                '1',
                '2'
            )
        )
    )
);
$this->ma_config = array(
    'groups' => array(
        'level 1' => array(
            'groups' => array(
                'level 2' => array()
            ),
            'values' => array(
                '1',
                '2'
            )
        )
    )
);
我应该以这样的方式结束:

$this->ma_config = array(
    'groups' => array(
        'level 1' => array(
            'values' => array(
                '1',
                '2'
            )
        )
    )
);
$this->ma_config = array(
    'groups' => array(
        'level 1' => array(
            'groups' => array(
                'level 2' => array()
            ),
            'values' => array(
                '1',
                '2'
            )
        )
    )
);
问题是,在我第二次通过循环时,当我运行line
/*MARKER*/
来创建第二个级别(即,应该导致
$this->ma_config['groups']['level 1]['groups']['level 2']=array()
)时,它会破坏“level 1”(从调试器看来,PHP最终会这样做
$this->ma_config['groups']['level1]=array()


<>为什么?

< p>我不太清楚你的函数是怎么做的,因为它有多么复杂。考虑使用这个简单的代码来扩充你的数据结构。然后,把任何操作或查找分成一个不同的函数。

<?php

$ma_config = array(
    'groups' => array(
        'level 1' => array(
            'values' => array(
                '1',
                '2'
            )
        )
    )
);

$ar_path = 'level 1';
$b_create = 'level 2';

$newGroup =& $ma_config['groups'][$ar_path]['groups'][$b_create];
$newGroup = (array) $newGroup;
unset($newGroup);

print_r($ma_config);

我测试了你的代码,它工作正常……你能把你的调用发布到
\uu组
看看你传递了什么参数吗?