Home >Backend Development >PHP Problem >How to check which row of an array a value is in in php
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.
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
<?php header('content-type:text/html;charset=utf-8'); $arr=array("id"=>"1","name"=>"李华","age"=>"25","color"=>"red"); var_dump($arr); $value=array_values($arr); $index=array_search("李华",$value)+1; echo "指定值 '李华' 在数组第 ".$index." 行"; ?>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!