Check if a specific value exists at a specific key in any subarray of a multidimensional array

前端 未结 13 2202
梦毁少年i
梦毁少年i 2020-11-27 18:42

I need to search a multidimensional array for a specific value in any of the indexed subarrays.

In other words, I need to check a single column of the multidimension

13条回答
  •  Happy的楠姐
    2020-11-27 19:29

    Try with this below code. It should be working fine for any kind of multidimensional array search.

    Here you can see LIVE DEMO EXAMPLE

    function multi_array_search($search_for, $search_in) {
        foreach ($search_in as $element) {
            if ( ($element === $search_for) ){
                return true;
            }elseif(is_array($element)){
                $result = multi_array_search($search_for, $element);
                if($result == true)
                    return true;
            }
        }
        return false;
    }
    

提交回复
热议问题