PHP array functions use the example series -- array lookup functions array_search and in_array

Source: Internet
Author: User
1. the array_searchmixedarray_search (mixed $ needle, array $ haystack [, bool $ strictfalse]) function searches for a given value in the array. if it succeeds, the corresponding key name is returned in & nbsp; haystack & nbsp; In Search 1. array_search
mixed array_search ( mixed $needle , array $haystack [, bool $strict = false ] )

Function

Search for the given value in the array. if the value is successful, the corresponding key name is returned.

Description

In?haystack? Search in?needle? Parameters.

Parameters

needle——Search value. (Note: If?needle? Is a string, the comparison is case sensitive)

haystack——This array.

strict——If the third parameter is optional?strict? Is it?TRUE, Then?Array_search ()? In?haystack? CheckIdentical. Which means the same check?haystack? In?needle? ? Type, and the object must be the same instance.

Return value

If yes?needle? Returns its key; otherwise, it returns?FALSE.

If?needle? In?haystack? Returns the first matched key. To return keys of all matched values, use? Array_keys ()? What are the optional parameters?search_value? .

II. in_array
bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )

Function

Check whether a value exists in the array.

Description

In?haystack? Search in?needle?strict? Then use loose comparison.

Parameters

needle——The value to be searched. (Note: If?needle? Is a string, the comparison is case sensitive)

haystack——This array.

strict——If the third parameter?strict? The value is?TRUE? Then?In_array ()? Will the function check?needle? Is the type?haystack? .

Return value

If you find?needle? ?TRUEOtherwise, return?FALSE.

III. Sample code
 "A", 2 => "B", 3 => "c"); $ arr3 = array (1 => "abc"); if (in_array ($ v, $ arr1) {echo "v in arr1 \ n";} else {echo "v not in arr1 \ n";} if (in_array ($ v, $ arr2 )) {echo "v in arr2 \ n";} else {echo "v not in arr2 \ n";} if (array_search ($ v, $ arr1 )) {echo "found v in arr1 \ n";} else {echo "found nothing \ n";} if (array_search ($ v, $ arr2 )) {echo "found v in arr2 \ n";} else {echo "found nothing \ n";} if (array_search ($ v, $ arr3 )){ Echo "found v in arr3 \ n";} else {echo "found nothing \ n" ;}?>

Print result:

v not in arr1v in arr2found nothingfound v in arr2found nothing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.