This may seem like a simple question but I have searched for an answer and come across call backs and a few different things but surely there is a simple way to do this.
I have a function in a class
that uses an iterative process so i end up with an array:
$msg[$i]
I want to exit the function with that array returned to main script
exit($msg[])
but will only return for example $msg[1]
or $msg[$i]
the last iteration I'd like to get the whole array without manually typing each one defeats the point of the iterative process
use return instead exit
return $msg;
have you tried using return $msg;
?
In order to return an array, you return the variable name of the array. This is really a pointer to the first element in the array.
This is an example of how to return an array:
function dofoo() {
$msg["a"] = "Foo";
$msg["b"] = "Bar";
$msg["c"] = "Baz";
return $msg;
}
$returned_array = dofoo();
Under the hood what happens is: return $msg;
returns a pointer to the first element of the array. $returned_array = dofoo();
allocates enough memory for the returned array and stores it in $returned_array
.