Home > Backend Development > PHP Tutorial > PHP Gets the Position of an Element in an Array Example_PHP Tutorial

PHP Gets the Position of an Element in an Array Example_PHP Tutorial

WBOY
Release: 2016-07-13 16:57:47
Original
927 people have browsed it

判断数组元素位置我们可以使用array_keys函数与foreach对比来实现,下面小编来给大家介绍介绍具体的实现实例。

array_keys的语法如下:

array_keys(array,value,[strict])

其中strict设置为true将触发数据类型校验的严格匹配模式,默认为false,下边的代码展示了array_keys的一个简单应用。

 代码如下 复制代码


$test=array(10,20,30,"10","20","30");
print_r(array_keys($test,"10",true));//严格匹配模式
//输出:
//Array ( [0] => 3)
?>

当然我们还可以使用foreach 加判断了

 代码如下 复制代码

$one = array(1, 2, 3, 4);

$two = array("test1" =>array('name'=>"tank","phone"=>12412433),
 "test2" => array('name'=>"ying","phone"=>252523523),
 "test3" => array('name'=>"zhang","phone"=>656452345),);

foreach ($one as $k=>$v) {
 echo "key=".$k."&value=".$v."<br>";
}
$i=0;
foreach ($two as $k=>$v) {

 foreach($v as $value){
  if( $v='tank' )
        {
           echo $i;
         }
          $i++;
 }
}

后面这个是利用遍历数组然后加一值的对比就可以判断出来了。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/631502.htmlTechArticle判断数组元素位置我们可以使用array_keys函数与foreach对比来实现,下面小编来给大家介绍介绍具体的实现实例。 array_keys的语法如下: array...
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