PHP array_search for multi dimensional array and return key

后端 未结 3 1372
误落风尘
误落风尘 2021-01-25 02:25

I\'m trying to search for a value in a multi dimensional array (below is only a part of the big array) and get the key for that value but I can\'t manage it by myself. Here is w

3条回答
  •  情歌与酒
    2021-01-25 03:17

    Try this function for recursive searching:

    function array_search_recursive($needle, array $haystack)
        {
            foreach ($haystack as $key => $value) {
                $current_key = $key;
    
                if ($needle === $value or (is_array($value) && array_search_recursive($needle, $value) !== false)) {
                    return $current_key;
                }
            }
            return false;
        }
    
    $key = array_search_recursive("FR*S30*E37*2*1", $data);
    

提交回复
热议问题