How to access array elements in php

王林
Release: 2023-02-25 19:18:01
Original
4521 people have browsed it

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]);
Copy after login

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 已经在数组中";
Copy after login

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);
Copy after login

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"]);  
}
Copy after login

The result obtained by executing this code:

apple's color is red
Copy after login

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])
Copy after login

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])
Copy after login

Program The running results are as follows:

watermelon was founded on green.
Copy after login

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])
Copy after login

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);
Copy after login

The program running results are as follows:

Array ( [0] => apple [1] => banana [2] => watermelon )
Copy after login

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)
Copy after login

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);
Copy after login

The program running result is as follows:

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template