Recipe 4.12. Finding the Position of a Value in an Array4.12.1. ProblemYou want to know if a value 4.12.2. SolutionUse array_search( ) $position = array_search($value, $array); 4.12.3. DiscussionUse in_array( ) $favorite_foods = array(1 => 'artichokes', 'bread', 'cauliflower', 'deviled eggs'); Use the If a value is in the array multiple times, array_search( ) is only guaranteed to return one of the instances, not the first instance. 4.12.4. See AlsoRecipe 4.11 for checking whether an element is in an array; documentation on array_search( ) at http://www.php.net/array-search; for more sophisticated searching of arrays using regular expression, see preg_replace( ), which is found at http://www.php.net/preg-replace and Chapter 22. |
Wednesday, January 20, 2010
Recipe 4.12. Finding the Position of a Value in an Array
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment