0

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?

stefanobaghino
  • 11,253
  • 4
  • 35
  • 63

2 Answers2

3

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

    }

}
Touheed Khan
  • 2,149
  • 16
  • 26
0

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();
pr1nc3
  • 8,108
  • 3
  • 23
  • 36
  • so how to get the value from those json in other function? – fahmi hammadi Feb 07 '18 at 08:22
  • to see the info of a json you need also to print_r it. If you want to treat it as an array you can json_decode it or simple you can access the json fields (that's on you). A small tip is when you decode you always use json_decode($myjson,true) to get an array and not an std class object. – pr1nc3 Feb 07 '18 at 08:24
  • actually i want to know how to use those array variable in other function – fahmi hammadi Feb 07 '18 at 08:32
  • I edited my post on how to call a function. How you want to use them it's up to you. As i said above even if it's json or array it's up to you on how you treat it. But you must follow the arrays rules. – pr1nc3 Feb 07 '18 at 08:33