$needle = 'foo';
$haystack = [
'bar' => [ 'foo' ],
'baz' => [ 'qux' ]
];
// if 'foo' in of the arrays of $haystack, return its key
// in this case its 'bar'
array_search
似乎在簡單數組上工作。我的情況需要什麼?array_search不適用於嵌套數組
$needle = 'foo';
$haystack = [
'bar' => [ 'foo' ],
'baz' => [ 'qux' ]
];
// if 'foo' in of the arrays of $haystack, return its key
// in this case its 'bar'
array_search
似乎在簡單數組上工作。我的情況需要什麼?array_search不適用於嵌套數組
希望這簡單的一個將有助於
foreach($haystack as $key => $array)
{
if(in_array($needle, $array))
{
break;
}
}
echo $key;
你需要一個簡單的'有一個可選的'foreach' break' –
檢查這個我的回答(從PHP文檔拍攝),它涉及重複的問題:https://stackoverflow.com/a/44205388/6726261 –