PHP multidimensional array search by value

前端 未结 23 3924
情歌与酒
情歌与酒 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:47

    Expanding on the function @mayhem created, this example would be more of a "fuzzy" search in case you just want to match part (most) of a search string:

     function searchArrayKeyVal($sKey, $id, $array) {
        foreach ($array as $key => $val) {
            if (strpos(strtolower($val[$sKey]), strtolower(trim($id))) !== false) {
                return $key;
            }
        }
             return false;
     }
    

    For example the value in the array is Welcome to New York! and you wanted the first instance of just "New York!"

提交回复
热议问题