检索传递给值PHP的数组键 [英] Retrieve array key passed on value PHP

查看:70
本文介绍了检索传递给值PHP的数组键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数组

$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
    }

}

推荐答案

您可以使用 array_search 函数以获取指定值的密钥:

You can use array_search function to get the key for specified value:

$key = array_search('gorilla', $group);

这篇关于检索传递给值PHP的数组键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆