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

青灯夜游
Release: 2023-03-15 21:20:01
Original
1956 people have browsed it

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:

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);
$arr=array("id"=>"1","name"=>"李华","age"=>"25","color"=>"red");
var_dump($arr);
$value=array_values($arr);
$index=array_search("李华",$value)+1;
echo "指定值 &#39;李华&#39; 在数组第 ".$index." 行";
?>
Copy after login

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!

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!