How to check if a student is in an array in php

PHPz
Release: 2023-04-27 10:32:04
Original
447 people have browsed it

php Query whether a student is in an array

When processing some student information, we often need to query whether a student is in a known student array. In this case, we can use the php language Some array functions are used for processing.

First, we assume that there is a student array named $students. The array contains information about multiple students, such as name, age, gender, etc., and each student information is in the form of an associative array. express.

Next we will use the in_array and array_column functions to determine whether a student is in the array.

in_array function

This function is used to determine whether a value is in an array. If true is returned, the value is in the array; if false is returned, the value is not in the array.

The syntax is as follows:

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

Parameter description:

$ needle: the value to be found;

$haystack: the array being searched;

$strict: optional parameter, the default is FALSE, indicating that strict mode is not used during comparison, that is, non-strict comparison . If this parameter is true, the data types will be compared in addition to the values.

The sample code is as follows:

$students = array(

array("name" => "张三", "age" => 20, "gender" => "男"),
array("name" => "李四", "age" => 22, "gender" => "女"),
array("name" => "王五", "age" => 18, "gender" => "男")
Copy after login
Copy after login

);

if(in_array("Zhang San", array_column($students, "name"))) {

echo "张三在学生数组中。";
Copy after login

} else {

echo "张三不在学生数组中。";
Copy after login

}

In this example, we first use the array_column function to get the names of all students in the student array, Then use the in_array function to determine whether "Zhang San" is in the student array. If it is, then output "Zhang San is in the student array."; otherwise, output "Zhang San is not in the student array."

array_column function

This function is used to return a specified column in the array . In our scenario, we often need to query a certain column of information in the student array, such as name, age, gender, etc., and we can use this function to process it.

The syntax is as follows:

array array_column ( array $input , mixed $column_key [, mixed $index_key = null ] )

Parameter description:

$ input: the multi-dimensional array (or result set) from which the array column needs to be retrieved;

$column_key: the column whose value needs to be returned, which can be a key name in the array or a numeric index;

$index_key: optional parameter, the index value of the new array is displayed by the $index_key column. This parameter can be the value of the column or the key name of the column.

The sample code is as follows:

$students = array(

array("name" => "张三", "age" => 20, "gender" => "男"),
array("name" => "李四", "age" => 22, "gender" => "女"),
array("name" => "王五", "age" => 18, "gender" => "男")
Copy after login
Copy after login

);

$names = array_column($students, "name");
print_r($names);

In this example, we use the array_column function to get the names of all students in the student array, and then use the print_r function to output the array.

Summary

The in_array function and array_column function in PHP both provide very convenient functions for fast query and operation in arrays. These two functions are undoubtedly very practical tools when dealing with scenarios where multi-dimensional arrays need to be processed.

The above is the detailed content of How to check if a student is in an array in php. For more information, please follow other related articles on the PHP Chinese website!

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!