PHP multidimensional array search by value

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

    Try this also

    function search_in_array($srchvalue, $array)
    {
        if (is_array($array) && count($array) > 0)
        {
            $foundkey = array_search($srchvalue, $array);
            if ($foundkey === FALSE)
            {
                foreach ($array as $key => $value)
                {
                    if (is_array($value) && count($value) > 0)
                    {
                        $foundkey = search_in_array($srchvalue, $value);
                        if ($foundkey != FALSE)
                            return $foundkey;
                    }
                }
            }
            else
                return $foundkey;
        }
    }
    
    0 讨论(0)
  • 2020-11-21 05:40
    function searchForId($id, $array) {
       foreach ($array as $key => $val) {
           if ($val['uid'] === $id) {
               return $key;
           }
       }
       return null;
    }
    

    This will work. You should call it like this:

    $id = searchForId('100', $userdb);
    

    It is important to know that if you are using === operator compared types have to be exactly same, in this example you have to search string or just use == instead ===.

    Based on angoru answer. In later versions of PHP (>= 5.5.0) you can use one-liner.

    $key = array_search('100', array_column($userdb, 'uid'));
    

    Here is documentation: http://php.net/manual/en/function.array-column.php.

    0 讨论(0)
  • 2020-11-21 05:40
    /**
     * searches a simple as well as multi dimension array
     * @param type $needle
     * @param type $haystack
     * @return boolean
     */
    public static function in_array_multi($needle, $haystack){
        $needle = trim($needle);
        if(!is_array($haystack))
            return False;
    
        foreach($haystack as $key=>$value){
            if(is_array($value)){
                if(self::in_array_multi($needle, $value))
                    return True;
                else
                   self::in_array_multi($needle, $value);
            }
            else
            if(trim($value) === trim($needle)){//visibility fix//
                error_log("$value === $needle setting visibility to 1 hidden");
                return True;
            }
        }
    
        return False;
    }
    
    0 讨论(0)
  • 2020-11-21 05:40

    Try this

    <?php
     function recursive_array_search($needle,$haystack) {
        foreach($haystack as $key=>$value) {
            $current_key=$key;
            if($needle===$value OR (is_array($value) && 
                recursive_array_search($needle,$value) !== false)) {
                 return $current_key;
            }
        }
        return false;
     }
     ?>
    
    0 讨论(0)
  • 2020-11-21 05:43
    for( $i =0; $i < sizeof($allUsers); $i++)
        {   
        $NEEDLE1='firstname';
        $NEEDLE2='emailAddress';
        $sterm='Tofind';
         if(isset($allUsers[$i][$NEEDLE1]) && isset($allUsers[$i][$NEEDLE2])
            {
                $Fname= $allUsers[$i][$NEEDLE1];
                $Lname= $allUsers[$i][$NEEDLE2];
    
                $pos1 = stripos($Fname, $sterm);
                $pos2=stripos($Lname, $sterm);//not case sensitive 
    
                if($pos1 !== false ||$pos2 !== false)
                {$resultsMatched[] =$allUsers[$i];}
                else
                {   continue;}              
            }
    
    }
    Print_r($resultsMatched); //will give array for matched values even partially matched
    

    With help of above code one can find any(partially matched) data from any column in 2D array so user id can be found as required in question.

    0 讨论(0)
  • 2020-11-21 05:44
    $search1 = 'demo';
    $search2 = 'bob';
    $arr = array('0' => 'hello','1' => 'test','2' => 'john','3' => array('0' => 'martin', '1' => 'bob'),'4' => 'demo');
    foreach ($arr as $value) { 
        if (is_array($value)) { 
            if (in_array($search2, $value)) { 
                echo "successsfully";    
                //execute your code 
            }
        } else {  
            if ($value == $search1) { 
                echo "success";
            }
        }
     }
    
    0 讨论(0)
提交回复
热议问题