PHP multidimensional array search by value

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

提交回复
热议问题