我正在尝试根据存储在其中的值来查找索引.
通常这很容易,但是我正在使用的数组是高度嵌套的.每个索引0,1,2都有字段f1,f2,f3.我试图找到哪个索引0,1,2在其f2字段中具有存储在其中的值.在这种情况下,它的索引为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";
解决方法:
In this case, it’s index 0. So that’s
the output I’m looking for.
$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
标签:arrays,php
来源: https://codeday.me/bug/20191209/2097161.html