fahmi hammadi
fahmi hammadi

Reputation: 78

Return array variable in PHP

I have a function and I'd like to return a variable to another function.

Can I return the array variable so I can use the variable at other function?

public function update_mdr_pameran() {
  //global $araydatamdr;
  $this->config->set_item('compress_output', FALSE);
  $araydatamdr['mdr_debit'] = trim($this->input->post('mdr_debit'));
  $araydatamdr['mdr_debit_npg'] = trim($this->input->post('mdr_debit_npg'));
  $araydatamdr['mdr_debit_pl'] = trim($this->input->post('mdr_debit_pl'));
  return $araydatamdr;
}

When I try to use $araydatamdr in another function, it became 0.

Am I missing something?

Upvotes: 0

Views: 932

Answers (2)

Touheed Khan
Touheed Khan

Reputation: 2151

You can achieve this by calling function and setting its return value to another variable.

Method 1 :

class Test extends CI_Controller {

        public function __construct() {
            parent::__construct();    
        }

        public function update_mdr_pameran() {
                //global $araydatamdr;
                $this->config->set_item('compress_output', FALSE);
                $araydatamdr['mdr_debit'] = trim($this->input->post('mdr_debit'));
                $araydatamdr['mdr_debit_npg'] = trim($this->input->post('mdr_debit_npg'));
                $araydatamdr['mdr_debit_pl'] = trim($this->input->post('mdr_debit_pl'));
                return $araydatamdr;
        }

        public function test_func() {
            $araydatamdr = $this->update_mdr_pameran();
            var_dump($araydatamdr);
        }

    }

Or you can also set $araydatamdr to $this reference.

Method 2 :

class Test extends CI_Controller {

    public $araydatamdr;
    public function __construct() {
        parent::__construct();    
        $this->araydatamdr = [];
    }

    public function update_mdr_pameran() {
            $this->config->set_item('compress_output', FALSE);
            $this->araydatamdr['mdr_debit'] = trim($this->input->post('mdr_debit'));
            $this->araydatamdr['mdr_debit_npg'] = trim($this->input->post('mdr_debit_npg'));
            $this->araydatamdr['mdr_debit_pl'] = trim($this->input->post('mdr_debit_pl'));
    }

    public function test_func() {
        $this->update_mdr_pameran();
        var_dump($this->araydatamdr);

    }

}

Upvotes: 3

pr1nc3
pr1nc3

Reputation: 8338

Cross out the echo $araydatamdr; Arrays can be printed using var_dump or print_r. Also you can return an array in php but personally i prefer to json_encode it first so i return a json as the output of my function something like:

return json_encode($araydatamdr);

Then it's a simple function call.

I don't know your project structure but i am giving general guidance. Apart from that i don't see anything else that could block your function.

I edit my post because i saw the issue is to call the function. There are 2 ways depending where you call it. If the function is in the same class as the other function you want to call it you simple go for :

$result=$this->update_mdr_pameran();

I see that your function has no arguments so you don't need to set any. If it's in another file:

1) include your php file at top like :

require 'myphpclass.php'; 

*tip make sure your path is right.

2) Create a new class object and then call the function like :

$class= new myClass();
$result=$class->update_mdr_pameran();

Upvotes: 0

Related Questions