Home>Article>Backend Development> How to access array elements in php

How to access array elements in php

王林
王林 Original
2019-10-08 09:23:01 4444browse

How to access array elements in php

PHP method of accessing array elements

in_array() function

in_array() function Searches an array for a specific value and returns true if the value is found, false otherwise. Its form is as follows:

boolean in_array(mixed needle,array haystack[,boolean strict]);

Look at the following example to find whether the variable apple is already in the array. If it is, output a piece of information:

$fruit = "apple"; $fruits = array("apple","banana","orange","pear"); if( in_array($fruit,$fruits) ) echo "$fruit 已经在数组中";

The third parameter is optional, it is mandatory in_array() considers types when searching.

array_key_exists() function

If a specified key is found in an array, the function array_key_exists() returns true, otherwise it returns false. Its form is as follows:

boolean array_key_exists(mixed key,array array);

The following example will search for apple in the array key. If found, the color of the fruit will be output:

$fruit["apple"] = "red"; $fruit["banana"] = "yellow"; $fruit["pear"] = "green"; if(array_key_exists("apple", $fruit)){ printf("apple's color is %s",$fruit["apple"]); }

The result obtained by executing this code:

apple's color is red

array_search() function

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

mixed array_search(mixed needle,array haystack[,boolean strict])

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

$fruits["apple"] = "red"; $fruits["banana"] = "yellow"; $fruits["watermelon"]="green"; $founded = array_search("green", $fruits); if($founded) printf("%s was founded on %s.",$founded, $fruits[$founded])

Program The running results are as follows:

watermelon was founded on green.

array_keys() function

array_keys() function returns an array containing all keys found in the searched array. It has the following form:

array array_keys(array array[,mixed search_value])

If the optional parameter search_value is included, only keys matching that value will be returned. The following example will output all arrays found in the $fruit array:

$fruits["apple"] = "red"; $fruits["banana"] = "yellow"; $fruits["watermelon"]="green"; $keys = array_keys($fruits); print_r($keys);

The program running results are as follows:

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 numerical index for the returned array. Its form is as follows:

array array_values(array array)

The following example will obtain the value of each element found in $fruits:

$fruits["apple"] = "red"; $fruits["banana"] = "yellow"; $fruits["watermelon"]="green"; $values = array_values($fruits); print_r($values);

The program running result is as follows:

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

Recommended tutorial:PHP video tutorial

The above is the detailed content of How to access array elements in php. For more information, please follow other related articles on the PHP Chinese website!

php
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:How to multi-thread in php Next article:How to multi-thread in php