PHP multidimensional array search by value

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

    Building off Jakub's excellent answer, here is a more generalized search that will allow the key to specified (not just for uid):

    function searcharray($value, $key, $array) {
       foreach ($array as $k => $val) {
           if ($val[$key] == $value) {
               return $k;
           }
       }
       return null;
    }
    

    Usage: $results = searcharray('searchvalue', searchkey, $array);

提交回复
热议问题