Is it possible to perform array search in php?

青灯夜游
Release: 2023-03-16 12:42:01
Original
1715 people have browsed it

Array search can be performed in php. PHP provides two array search functions: 1. array_search(), which can search for a given value in the array and return the corresponding key name. The syntax "array_search (search value, array, whether to compare types when searching)" returns The value is of string type; 2. array_keys(), used to search for a given value in the array and return the corresponding key name. The syntax is "array_keys (array, search value, comparison type)", and the return value is an array type.

Is it possible to perform array search in php?

The operating environment of this tutorial: Windows 7 system, PHP version 8.1, DELL G3 computer

Array search can be performed in php.

There are two array search functions provided in php:

  • array_search()

  • array_keys()

Both functions can search for a given value in an array and return the corresponding key name (subscript). The difference is: the return value of array_search() is of string type, while the return value of array_keys() is of array type.

1. Use array_search() for array search

array_search() function searches for a key value in the array and returns the corresponding key name.

array_search(value,array,strict)
Copy after login
ParametersDescription
valueRequired . Specifies the key value to search for in the array.
arrayRequired. Specifies the array to be searched.
strict Optional. If this parameter is set to TRUE, the function searches the array for elements of the same data type and value. Possible values:
  • true
  • false - default
If set to true, the type of the given value in the array is checked, the number 5 and the string 5 are different (see Example 2).

Example: Search for the key value "red" in the array and return its key name

<?php
header("Content-type:text/html;charset=utf-8");
$a=array("a"=>"red","b"=>"green","c"=>"blue");
var_dump($a);
echo array_search("red",$a);
?>
Copy after login

Is it possible to perform array search in php?

2. Use array_keys() for array search

array_key() function can obtain some or all key names in the array.

array_keys(array,value,strict)
Copy after login

Array search can only be performed when the value parameter is set,

Parameter Description
arrayRequired. Specifies an array.
value Optional. You can specify a key value, and then only the key name corresponding to that key value will be returned.
strict Optional. Used with the value parameter. Possible values:
  • true - Returns the key name with the specified key value. Depending on the type, the number 5 is not the same as the string "5".
  • false - the default value. Independent of type, the number 5 is the same as the string "5".

Example: Search for the key value "Highlander" in the array and return its key name

<?php
header("Content-type:text/html;charset=utf-8");
$a=array("Volvo"=>"XC90","BMW"=>"X5","Toyota"=>"Highlander");
var_dump($a);
var_dump(array_keys($a,"Highlander"));
?>
Copy after login

Is it possible to perform array search in php?

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of Is it possible to perform array search 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!