PHP Array method

Source: Internet
Author: User

Finding, filtering, and searching array elements are some of the common features of array manipulation. Here are a few related functions.

In_array () function

The In_array () function searches for a specific value in an array rollup and returns True if this value is found, otherwise false. The form is as follows:

1 boolean in_array(mixed needle,arrayhaystack[,boolean strict]);

Look at the following example, look for the variable if Apple is already in the array, and if so, output a piece of information:

1 $fruit"apple";
2 $fruitsarray("apple","banana","orange","pear");
3 if( in_array($fruit,$fruits) )
4     echo"$fruit 已经在数组中";

The third parameter is optional, which forces In_array () to consider the type when searching.

Array_key_exists () function

If a specified key is found in an array, the function array_key_exists () returns True, otherwise false is returned. The form is as follows:

1 boolean array_key_exists(mixed key,arrayarray);

The following example searches for Apple in the array key and, if found, outputs the color of the fruit:

1 $fruit["apple"] = "red";
2 $fruit["banana"] = "yellow";
3 $fruit["pear"] = "green";
4 if(array_key_exists("apple"$fruit)){
5     printf("apple‘s color is %s",$fruit["apple"]);
6 }

The result of executing this code:

1 apple‘s color is red
Array_search () function

The Array_search () function searches for a specified value in an array, returns the corresponding key if found, otherwise returns false. The form is as follows:

1 mixed array_search(mixed needle,arrayhaystack[,boolean strict])

The following example searches for a specific date in $fruits (December 7) and, if found, returns information about the corresponding state:

1 $fruits["apple"] = "red";
2 $fruits["banana"] = "yellow";
3 $fruits["watermelon"]="green";
4 $foundedarray_search("green"$fruits);
5 if($founded)
6     printf("%s was founded on %s.",$founded$fruits[$founded])

The results of the program run as follows:

1 watermelon was founded on green.
Array_keys () function

The Array_keys () function returns an array that contains all the keys found in the searched array. The form is as follows:

1 arrayarray_keys(arrayarray[,mixed search_value])

If the optional parameter search_value is included, only the key that matches the value is returned. The following example outputs all the arrays found in the $fruit array:

1 $fruits["apple"] = "red";
2 $fruits["banana"] = "yellow";
3 $fruits["watermelon"]="green";
4 $keysarray_keys($fruits);
5 print_r($keys);

The results of the program run as follows:

1 Array ( [0] => apple [1] => banana [2] => watermelon )
Array_values () function

The Array_values () function returns all the values in an array and automatically provides a numeric index for the returned array. The form is as follows:

1 arrayarray_values(arrayarray)

The following example gets the values of each element found in $fruits:

1 $fruits["apple"] = "red";
2 $fruits["banana"] = "yellow";
3 $fruits["watermelon"]="green";
4 $valuesarray_values($fruits);
5 print_r($values);

The results of the program run as follows:

1 Array ( [0] => red [1] => yellow [2] => green )

Original link: http://www.nowamagic.net/librarys/posts/php/44

PHP Array method

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.