0

I'm not really sure how to ask this question which is why I think I can't find a suitable answer...

I have a simple function and inside that function I'm assigning an existing array to a variable. By changing the variable, I would like to update the array as well. I'm well aware that I can do it by pushing the variable data back out to the array but I'm curious if I can use this in a more simple fashion... almost as if I'm storing the variable as a value instead of a reference to the array.

Here's the function with my notes

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);
    }

It shouldn't make much of a difference but here's $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()
    );

Just curious if it's possible - thanks!

1 Answer 1

2

Yes, but it's not common. You can do this:

$f = &$this->form;

And remove this

$this->form = $f;

Detailed information at docs.

Sign up to request clarification or add additional context in comments.

2 Comments

Might not be common but good to have in my arsenal. Thank you
Always good to know what is possible, we never know when we'll need things :) Good luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.