获取与状态键在PHP数组 [英] Get key with condition in Array in PHP

查看:163
本文介绍了获取与状态键在PHP数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 if ($totalModuletest>0){
     if (in_array(1, $modValArr, true)){
         echo "1.13 found with strict check\n";
      }
 }
 else{
      $aVal = 0;
 }

使用的print_r($ modValArr);

By using print_r($modValArr);

Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 1[5])

我想知道的任何值这个数组中存在大于零。如果它存在,我需要它的关键。我需要的结果是4。

I want to know any value exist greater than zero in this array. If it exists I need its key. The result I need is 4.

如何在PHP这可能吗?

How is this possible in PHP?

推荐答案

这应该为你工作:

(在这里,我只是过滤低于0的所有值与出 array_filter() ,然后只需用获得的 array_keys()

(Here I just filter all values below 0 out with array_filter() and then just get the keys with array_keys())

<?php

    $arr = [0, 0, 0, 0, 1, ""];
    $result = array_keys(array_filter($arr, function($v){
        return $v > 0;
    }));

    print_r($result);

?>

输出:

Array ( [0] => 4 )

这篇关于获取与状态键在PHP数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆