Home>Article>Backend Development> How to check which row of an array a value is in in php

How to check which row of an array a value is in in php

青灯夜游
青灯夜游 Original
2022-04-28 19:19:17 1890browse

Method to check the number of rows: 1. Use the "array_values($arr)" statement to convert the specified array into an index array; 2. Use the "array_search("value", index array)" statement in the index array Search for the specified value and return the corresponding index; 3. Use the "index value 1" statement to calculate the number of rows of the specified value in the array.

How to check which row of an array a value is in in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

php check value is in Which row of the array

Implementation method:

  • Use array_values() to get the values of all elements in the array, and return a value containing all key values array, that is, convert the array into an index array.

  • Use array_search() to search for a given value in the index array and return the corresponding index value

  • The array index value starts from 0 , so the number of rows with the specified value needs to be added to the obtained index value by adding 1

    How to check which row of an array a value is in in php

##Implementation example:

"1","name"=>"李华","age"=>"25","color"=>"red"); var_dump($arr); $value=array_values($arr); $index=array_search("李华",$value)+1; echo "指定值 '李华' 在数组第 ".$index." 行"; ?>

How to check which row of an array a value is in in php

Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of How to check which row of an array a value is in 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