php - How to return control from callback function or break the processing of array in middle array_filter processing -


can break execution of callback once condition satisfied 1 element of array?

ex .

$a = array(1,2,3,4,5); foreach($a $val){      if ($val == 3){       break;     }  } 

if write call it, below

$result = array_filter($a, function(){       if ($val == 3){           return true;         } }); 

in callback go through array element, in spite of condition being satisfied @ 3. rest 2 elements 4, 5 go through callback

i want such function in callback, break callback 1 desired condition match , stop execution of rest of elements

is possible?

you can a static variable. static variable of local scope inside callback function preserves value between calls.

it behaves global variable in terms of value, local scope:

$callback = function($val) {     static $filter = false;      if ($val == 3) {         $filter = true;     }      return $filter;         }; 

this callback return false until $val == 3. return true.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -