在函数名中使用引用运算符会返回通知


Using reference operator in function name returns notices

我的功能:

function &get_element_from_array(&$array, $searchValue){    
    foreach($array as $id => &$subtree) {
        if ($id === $searchValue) {
            return $subtree;
        }
        if (isset($subtree['children'])) {
            $subsearch = &$this->get_element_from_array($subtree['children'], $searchValue);
            if ($subsearch !== false) {
                return $subsearch;
            }
        }
    }
    return false;
}

我有一个像这样的数组:

$table = [
1 => [
    'id' => 1,
    'children_count' => 0,
    'visited' => 1,
    'children_visited' => 0
],
2 => [
    'id' => 2,
    'children_count' => 0,
    'visited' => 1,
    'children_visited' => 0,
    'children' => [
        3 => [
            'id' => 3,
            'children_count' => 0,
            'visited' => 1,
            'children_visited' => 0,
            'children' => [
                4 => [
                    'id' => 4,
                    'children_count' => 0,
                    'visited' => 1,
                    'children_visited' => 0,
                    'children' => [
                        5 => [
                            'id' => 5,
                            'children_count' => 0
                            'visited' => 0,
                            'children_visited' => 0
                        ],
                        6 => [
                            'id' => 6,
                            'children_count' => 0
                            'visited' => 1,
                            'children_visited' => 0
                        ]
                    ]
                ]
            ]
        ]
    ]
]
];

此函数按预期工作。问题是,它向我发送通知:

消息:引用

只能返回变量引用

函数名中的引用操作符存在问题。函数不能工作,如果我删除&注意停止:)

我发送一些数据回POST调用后,我完成这个函数和所有这些通知被发送回javascript:(

你们有什么建议?

解决方案是始终返回一个变量。

function &get_element_from_array(&$array, $searchValue){    
    $result = false;
    foreach($array as $id => &$subtree) {
        if ($id === $searchValue) {
            return $subtree;
        }
        if (isset($subtree['children'])) {
            $subsearch = &$this->get_element_from_array($subtree['children'], $searchValue);
            if ($subsearch !== false) {
                return $subsearch;
            }
        }
    }
    return $result;
}