-
四季花海
只需循环数组并检查元素是否存在:function search(string $search, array $source) { foreach ($source as $key => $sub) { if (in_array($search, $sub)) { return $key; } }}search('apple', $source);
-
慕哥6287543
只需迭代所有子数组并返回父键即可。$array = [ 'example1' => ['banana'], 'example2' => ['orange', 'apple', 'plum', 'watermelon', 'pumpkin'], 'example3' => ['cherry', 'strawberry']];$needle = 'apple';$result = '';foreach($array as $parentKey => $child) { if(in_array($needle, $child)) { $result = $parentKey; break; }}echo $result;例子2
-
蓝山帝景
这也是一个很好的例子,谢谢。我在此基础上构建了我的功能function CheckArray($my_array,$search) {$result = array_keys(array_filter($my_array, function ($arr) use ($search) {return in_array($search, $arr);}));if(isset($result[0])){ return $result[0];}else{ return "not find";}}