PHP multidimensional array search by value

前端 未结 23 3331
情歌与酒
情歌与酒 2020-11-21 04:45

I have an array where I want to search the uid and get the key of the array.

Examples

Assume we have the following 2-dimensional array:

<
23条回答
  •  爱一瞬间的悲伤
    2020-11-21 05:29

    Just share, maybe can like this.

    if( ! function_exists('arraySearchMulti')){
    function arraySearchMulti($search,$key,$array,$returnKey=false)
    {
        foreach ($array as $k => $val) {
            if (isset($val[$key])) {
                if ((string)$val[$key] == (string)$search) {
                    return ($returnKey ? $k : $val);
                }
            }else{
                return (is_array($val) ? arraySearchMulti($search,$key,$val,$returnKey) : null);
            }
        }
        return null;
    }}
    

提交回复
热议问题