0

I have an each loop function, i want to return an array outside the function.

//tried defining the array result outside the function, but whenever i printed the array it returning a empty array.

$test->each(function()){
//tried defining the array result inside the function, but whenever i printed the array its returning a undefined array.
   $result[] = "test";
}
echo json_encode($result);
//print array Undefined variable: result

EDIT: i think when you use a closure in PHP, the variable inside the function is isolated. i need to convert it into foreach function.

Ctrlex
  • 61
  • 1
  • 13

1 Answers1

0

When you use a closure in PHP, the variable inside the function is isolated. You need to convert it into foreach function so you can assign a value to array without a problem.

Ctrlex
  • 61
  • 1
  • 13