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?

13
  • How do you call $araydatamdr at other function? Commented Feb 7, 2018 at 8:07
  • You don't need to echo and return. Just return it. Commented Feb 7, 2018 at 8:07
  • Echo on array doesn't work, use var_dump($array);. Commented Feb 7, 2018 at 8:08
  • 1
    Possible duplicate of PHP function return array Commented Feb 7, 2018 at 8:10
  • 2
    Impossible. You should call it with something like $arr = update_mdr_pameran() Commented Feb 7, 2018 at 8:16

2 Answers 2

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

    }

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

Comments

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

4 Comments

so how to get the value from those json in other function?
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.
actually i want to know how to use those array variable in other function
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.

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.