1

i built a function to charge some values to an array. The thing is that i need to get that array (which is inside a function) from another function.

Don't know how to deal with the parameters.

this is the function where i charge my array:

public function loadStates(){
    $states = array(
        "Buenos Aires" => "label label-success",
        "Catamarca" => "label label-info",
        "Chaco" => "label label-warning",
        "Chubut" => "label label-danger";
    );

    return $states;
}

this is the other function (i pretend to call to the function that creates the array so i can load the data to use in the current function):

public function countUsers() {
    //breadcrumb
    $this->data["states"] = loadStates();
    var_dump('$this->data["states"]');die();
    //$this->data['totUsers'] = UsersDs::getInstance()->count();
    //$this->parser->parse('admin/usuarios/totales/totalRegUsers.tpl',$this->data);
} 

these functions are in the same php file. I have erased all the function arguments, so obviously this is not working

4
  • 3
    add a code snippet of what have you tried Commented Sep 11, 2013 at 18:12
  • This is what function arguments are for. Commented Sep 11, 2013 at 18:14
  • @Sammitch thanks, but that's not helping. I know about that, just don't know how to use them Commented Sep 11, 2013 at 18:25
  • Maybe I'm missing something, but it seems like the code you posted should work?? What is it not doing that you expect it to do? Commented Sep 11, 2013 at 18:47

2 Answers 2

1

You have two options here that I immediately see (really there are more, but hard to know what is best in your case). You can either pass the array by reference to the method, or you can return the new array from the loadStates() method.

Method 1: Pass by Reference

public function loadStates(&$arr) {
    $arr = array(
        "Buenos Aires" => "label label-success",
        "Catamarca" => "label label-info",
        "Chaco" => "label label-warning",
        "Chubut" => "label label-danger"
    );
}

public function countUsers(){
    $this->loadStates($this->data["states"]);
    var_dump($this->data["states"]);die();
} 

Method 2: Return new array

public function loadStates() {
    return array(
        "Buenos Aires" => "label label-success",
        "Catamarca" => "label label-info",
        "Chaco" => "label label-warning",
        "Chubut" => "label label-danger"
    );
}

public function countUsers(){
    $this->data["states"] = $this->loadStates();
    var_dump($this->data["states"]);die();
} 
Sign up to request clarification or add additional context in comments.

4 Comments

the thing is that my array is inside another function, in that case how would this be? (in total, i have 3 functions)
I feel like the code you posted above in your question should work. What am I missing?
it was missing the $this reference: $this->loadStates(); So now thanks to you it's working! i used option number 2.
Ah, well glad to help! That must be because it's part of a class.
1

You have several options here ....

1) Return the array from the first function

public function loadStates(){
return array(
    "Buenos Aires" => "label label-success",
    "Catamarca" => "label label-info",
    "Chaco" => "label label-warning",
    "Chubut" => "label label-danger";
             );
}

This will recreate and return an array every time the loadStates is invoked.

2) Use static assignment in the function then return.

public function loadStates(){
static $states = array(
    "Buenos Aires" => "label label-success",
    "Catamarca" => "label label-info",
    "Chaco" => "label label-warning",
    "Chubut" => "label label-danger";
             );
    return $states;
}

This will create the array the first time the function is invoked and return the stored copy on subsequent calls.

3) Assign the $states to a property during construction of your object and use the a property accessor to get the array

public function __construct(){
$this->states = array(
    "Buenos Aires" => "label label-success",
    "Catamarca" => "label label-info",
    "Chaco" => "label label-warning",
    "Chubut" => "label label-danger";
             );
}

Then in your code

$this->data["states"] = $this->states();

4) Lazily load a static the property at construction.

static protected $states;

public function __construct(){
if (!is_array(self::$states)) 
    self::$states = array(
    "Buenos Aires" => "label label-success",
    "Catamarca" => "label label-info",
    "Chaco" => "label label-warning",
    "Chubut" => "label label-danger";
    );
}

Then when you want to access the property from within use syntax like this:

self::$states["Buenos Aires"]

There are several other options but which one that should be used is completely dependent upon your particular use case.

1 Comment

thanx for the dedication, really appreciate, i will try this options

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.