假设我给你以下数组:
$array = array (
array (
'key' => 'value'
),
array (
'key' => 'another value'
),
array (
'key' => 'yet another value'
),
array (
'key' => 'final value'
)
);
我想要从这个数组中得到的是具有所提供值的数组的位置。因此,如果我在 $array
中搜索 "yet another value"
的值,结果应该是: 2
因为匹配的数组位于位置2.
我知道要执行以下操作:
foreach ($array as $a) {
foreach ($a as $k => $v) {
if ($v === "yet another value") {
return $array[$a]; // This is what I would do, but I want
// the int position of $a in the $array.
// So: $a == 2, give me 2.
}
}
}
更新:
key 始终相同
最佳答案
如果 key 相同,您可以简单地执行此操作:
$v = "yet another value";
$a = array_search(array("key"=>$v),$array);
echo $a;//2
http://sandbox.onlinephpfunctions.com/code/1c37819f25369725effa5acef012f9ce323f5425
关于php - 返回包含数组的数组位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30763018/