在 PHP 中的数组中获取带条件的键

Son*_*ona 1 php arrays

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

通过使用 print_r($modValArr);

Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 1[5])
Run Code Online (Sandbox Code Playgroud)

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

这在 PHP 中怎么可能?

Riz*_*123 5

这应该适合你:

(这里我只是用 过滤掉所有低于 0 的值array_filter(),然后用 获取键array_keys()

<?php

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

    print_r($result);

?>
Run Code Online (Sandbox Code Playgroud)

输出:

Array ( [0] => 4 )
Run Code Online (Sandbox Code Playgroud)