Giving my function access to outside variable

前端 未结 6 1668
梦毁少年i
梦毁少年i 2020-11-22 14:32

I have an array outside:

$myArr = array();

I would like to give my function access to the array outside it so it can add values to it

6条回答
  •  长发绾君心
    2020-11-22 15:11

    Two Answers

    1. Answer to the asked question.

    2. A simple change equals a better way!

    Answer 1 - Pass the Vars Array to the __construct() in a class, you could also leave the construct empty and pass the Arrays through your functions instead.

    content_arrays = $content_arrays;
    
        }
    
        // Push Values from in the Array using Public Function
        public function array_push_1(){
    
            // Values
            $this->push_values_1 = array(1,"2B or not 2B",3,"42",5);
            $this->push_values_2 = array("a","b","c");
    
            // Loop Values and Push Values to Sub Array
            foreach($this->push_values_1 as $this->values){
    
                $this->content_arrays["js_custom"][] = $this->values;
    
            }
    
            // Loop Values and Push Values to Sub Array
            foreach($this->push_values_2 as $this->values){
    
                $this->content_arrays["modals"][] = $this->values;
    
            }
    
        // Return Primary Array with New Values
        return $this->content_arrays;
    
        }
    
        // GET Push Values External to the Class with Public Function
        public function array_push_2($external_values){
    
            $this->push_values_3 = $external_values["values_1"];
            $this->push_values_4 = $external_values["values_2"];
    
            // Loop Values and Push Values to Sub Array
            foreach($this->push_values_3 as $this->values){
    
                $this->content_arrays["js_custom"][] = $this->values;
    
            }
    
            // Loop Values and Push Values to Sub Array
            foreach($this->push_values_4 as $this->values){
    
                $this->content_arrays["modals"][] = $this->values;
    
            }
    
        // Return Primary Array with New Values
        return $this->content_arrays;
    
        }
    
    }
    
    // Start the Class with the Contents Array as a Parameter
    $content_arrays = new Array_Pushing_Example_1($content_arrays);
    
    // Push Internal Values to the Arrays
    $content_arrays->content_arrays = $content_arrays->array_push_1();
    
    // Push External Values to the Arrays
    $external_values = array();
    $external_values["values_1"] = array("car","house","bike","glass");
    $external_values["values_2"] = array("FOO","foo");
    $content_arrays->content_arrays = $content_arrays->array_push_2($external_values);
    
    // The Output
    echo "Array Custom Content Results 1";
    echo "
    "; echo "
    "; echo "Modals - Count: ".count($content_arrays->content_arrays["modals"]); echo "
    "; echo "-------------------"; echo "
    "; // Get Modals Array Results foreach($content_arrays->content_arrays["modals"] as $modals){ echo $modals; echo "
    "; } echo "
    "; echo "JS Custom - Count: ".count($content_arrays->content_arrays["js_custom"]); echo "
    "; echo "-------------------"; echo "
    "; // Get JS Custom Array Results foreach($content_arrays->content_arrays["js_custom"] as $js_custom){ echo $js_custom; echo "
    "; } echo "
    "; ?>

    Answer 2 - A simple change however would put it inline with modern standards. Just declare your Arrays in the Class.

    content_arrays["modals"] = array();
            $this->content_arrays["js_custom"] = array();
    
        }
    
        // Push Values from in the Array using Public Function
        public function array_push_1(){
    
            // Values
            $this->push_values_1 = array(1,"2B or not 2B",3,"42",5);
            $this->push_values_2 = array("a","b","c");
    
            // Loop Values and Push Values to Sub Array
            foreach($this->push_values_1 as $this->values){
    
                $this->content_arrays["js_custom"][] = $this->values;
    
            }
    
            // Loop Values and Push Values to Sub Array
            foreach($this->push_values_2 as $this->values){
    
                $this->content_arrays["modals"][] = $this->values;
    
            }
    
        // Return Primary Array with New Values
        return $this->content_arrays;
    
        }
    
        // GET Push Values External to the Class with Public Function
        public function array_push_2($external_values){
    
            $this->push_values_3 = $external_values["values_1"];
            $this->push_values_4 = $external_values["values_2"];
    
            // Loop Values and Push Values to Sub Array
            foreach($this->push_values_3 as $this->values){
    
                $this->content_arrays["js_custom"][] = $this->values;
    
            }
    
            // Loop Values and Push Values to Sub Array
            foreach($this->push_values_4 as $this->values){
    
                $this->content_arrays["modals"][] = $this->values;
    
            }
    
        // Return Primary Array with New Values
        return $this->content_arrays;
    
        }
    
    }
    
    // Start the Class without the Contents Array as a Parameter
    $content_arrays = new Array_Pushing_Example_2();
    
    // Push Internal Values to the Arrays
    $content_arrays->content_arrays = $content_arrays->array_push_1();
    
    // Push External Values to the Arrays
    $external_values = array();
    $external_values["values_1"] = array("car","house","bike","glass");
    $external_values["values_2"] = array("FOO","foo");
    $content_arrays->content_arrays = $content_arrays->array_push_2($external_values);
    
    // The Output
    echo "Array Custom Content Results 1";
    echo "
    "; echo "
    "; echo "Modals - Count: ".count($content_arrays->content_arrays["modals"]); echo "
    "; echo "-------------------"; echo "
    "; // Get Modals Array Results foreach($content_arrays->content_arrays["modals"] as $modals){ echo $modals; echo "
    "; } echo "
    "; echo "JS Custom - Count: ".count($content_arrays->content_arrays["js_custom"]); echo "
    "; echo "-------------------"; echo "
    "; // Get JS Custom Array Results foreach($content_arrays->content_arrays["js_custom"] as $js_custom){ echo $js_custom; echo "
    "; } echo "
    "; ?>

    Both options output the same information and allow a function to push and retrieve information from an Array and sub Arrays to any place in the code(Given that the data has been pushed first). The second option gives more control over how the data is used and protected. They can be used as is just modify to your needs but if they were used to extend a Controller they could share their values among any of the Classes the Controller is using. Neither method requires the use of a Global(s).

    Output:

    Array Custom Content Results

    Modals - Count: 5

    a

    b

    c

    FOO

    foo

    JS Custom - Count: 9

    1

    2B or not 2B

    3

    42

    5

    car

    house

    bike

    glass

提交回复
热议问题