根据值查找数组索引

时间:2010-08-17 23:38:35

标签: php arrays

我正在尝试根据存储在其中的值来查找索引。

这通常很容易,但我正在使用的数组是高度嵌套的。每个索引0,1,2都有字段f1,f2,f3。我正在尝试查找哪个索引0,1,2在其f2字段中存储的值this。在这种情况下,它是索引0。这就是我正在寻找的输出。在PHP中有一个巧妙的技巧来有效地做到这一点吗?

$somearray[0][f1] = "not this";
$somearray[0][f2] = "this"; 
$somearray[0][f3] = "not this"; 

$somearray[1][f1] = "not this";
$somearray[1][f2] = "not this";
$somearray[1][f3] = "not this"; 

$somearray[2][f1] = "not this"; 
$somearray[2][f2] = "not this"; 
$somearray[2][f3] = "not this"; 

1 个答案:

答案 0 :(得分:3)

  

在这种情况下,它的索引是0.所以那就是   我正在寻找的输出。

$somearray[0]['f1'] = "not this";
$somearray[0]['f2'] = "this";
$somearray[0]['f3'] = "not this";

$somearray[1]['f1'] = "not this";
$somearray[1]['f2'] = "not this";
$somearray[1]['f3'] = "not this";

$somearray[2]['f1'] = "not this";
$somearray[2]['f2'] = "not this";
$somearray[2]['f3'] = "not this";

foreach($somearray as $key => $value)
{
  if($value['f2'] === 'this')
  {
     echo $key; // find out the key
  }
}

<强>输出:

0
相关问题