【发布时间】:2023-03-10 17:04:02
【问题描述】:
我不确定如何问这个问题,这就是为什么我认为我找不到合适的答案...
我有一个简单的函数,在该函数中我将一个现有数组分配给一个变量。通过更改变量,我也想更新数组。我很清楚我可以通过将变量数据推回数组来做到这一点,但我很好奇是否可以以更简单的方式使用它......几乎就像我将变量存储为值一样而不是对数组的引用。
这是我的笔记的功能
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 -> 表单
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()
);
只是好奇是否可能 - 谢谢!
【问题讨论】: