There is an array.

$report = array(); $report ['tasks'] = [ 'report_field' => '1', 'report' => function () { $i = 'ok'; return $i; } ]; 

It is necessary, using a closure, to assign a value to the key. In this case, report If you do a var_dump array, then I get object (Closure) # 1 (0) {} If I do echo $ report ['tasks'] ['report'] , then I get Error 500. Why then is there a closure in PHP or I don't understand something? For me, the utility of closures would be in the ability to process data before assigning them to an array key.

  • you probably don’t understand what a closure is) - madfan41k
  • 2
    print_r ($ report ['tasks'] ['report'] ()); - madfan41k
  • it turns out that through the closure I can not assign a value to the array key? I need here $ report ['tasks'] ['report'] to have meaning - the result of the work of the function - Vitaut Hryharovich

1 answer 1

 <?php $report = [ 'tasks' => [ 'report_field' => 1, 'report' => function() { echo 'test string'; } ] ]; $call = $report['tasks']['report']; $call(); 
  • $call(); or echo $call(); - Let's say Pie
  • Still not the way I wanted. For some reason, I got the impression that the closure can be used in arrays. In general, I solved my question by processing data before the array, and not in the array. Besides, I convert the array to json and give it over the network. - Vitaut Hryharovich
  • json_encode does not support closures that are made in an array - Let's say Pie