Retrieve array key passed on value PHP
问题 I have the following array $group= array( [0] => 'apple', [1] => 'orange', [2] => 'gorilla' ); I run the array group through an for each function and when the loop hits values of gorilla I want it to spit out the index of gorilla foreach ($group as $key) { if ($key == gorilla){ echo //<------ the index of gorilla } } 回答1: You can use array_search function to get the key for specified value: $key = array_search('gorilla', $group); 回答2: foreach( $group as $index => $value) { if ($value ==