Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php变量作为值而不是引用_Php_Arrays_Variables - Fatal编程技术网

php变量作为值而不是引用

php变量作为值而不是引用,php,arrays,variables,Php,Arrays,Variables,我真的不知道如何问这个问题,这就是为什么我认为我找不到一个合适的答案 我有一个简单的函数,在该函数中,我将现有数组赋值给一个变量。通过更改变量,我也想更新数组。我很清楚,我可以通过将变量数据推回数组来实现这一点,但我很好奇是否可以以更简单的方式使用它。。。就像我将变量存储为一个值而不是数组的引用一样 这是我的笔记的功能 function __construct($name, $action, $a){ # $a accepts a series of multidimensional sub-a

我真的不知道如何问这个问题,这就是为什么我认为我找不到一个合适的答案

我有一个简单的函数,在该函数中,我将现有数组赋值给一个变量。通过更改变量,我也想更新数组。我很清楚,我可以通过将变量数据推回数组来实现这一点,但我很好奇是否可以以更简单的方式使用它。。。就像我将变量存储为一个值而不是数组的引用一样

这是我的笔记的功能

function __construct($name, $action, $a){ # $a accepts a series of multidimensional sub-arrays
        $f = $this -> form; #passing the reference in here
        $f['name'] = $name; #ref 
        $f['action'] = $action; #ref 
        foreach($a as $k => $v){
            if(is_array($f[$k])){
                array_push($f[$k], $v);
            }else{
                $f[$k] = $v;
            }
        }
        # $f now contains a new value however I want to know if it's possible to make $f directly change $this -> form without a back reference
        $this -> form = $f; #this solves the problem but is there a better way?
        var_dump($this -> form);
    }
它应该不会有太大的区别,但这里有$this->form

protected $form = array(
        "title" => "",
        "name" => "",
        "id" => "",
        "class" => "Frm-cb", #default class for all forms
        "action" => "",
        "method" => "POST",
        "rel" => "",
        "topmsg" => "",
        "autocomplete" => "",
        "inputs" => array(),
        "buttons" => array(),
        "props" => array(),
        "attributes" => array()
    );

只是好奇,如果可能的话-谢谢

是的,但这并不常见。您可以这样做:

$f = &$this->form;
把这个拿走

$this->form = $f;

详细信息位于。

可能不常见,但在我的武器库中很有用。谢谢你总是很高兴知道什么是可能的,我们永远不知道什么时候我们会需要东西:)祝你好运!