4.14. Finding Elements That Pass a Certain Test4.14.2. Solution
4.14.3. DiscussionThe foreach loops are simple; you scroll through the data and append elements to the return array that match your criteria. If you want only the first such element, exit the loop using break :
You can also return directly from a function:
With array_filter( ), however, you first create a callback function that returns true for values you want to keep and false for values you don't. Using array_filter( ), you then instruct PHP to process the array as you do in the foreach. It's impossible to bail out early from array_filter( ), so foreach provides more flexibility and is simpler to understand. Also, it's one of the few cases in which the built-in PHP function doesn't clearly outperform user-level code. 4.14.4. See AlsoDocumentation on array_filter( ) at http://www.php.net/array-filter.
Copyright © 2003 O'Reilly & Associates. All rights reserved. |
|