开发者

Searching for array index in a multi dimentional array

I have an array like

Array ( 
     [0] 开发者_开发百科=> Array ( [0] => Array ( [SSN] => 123456789 [borrowerName] => Peter K  ) ) 
     [1] => Array ( [0] => Array ( [SSN] => 412516789 [borrowerName] => Jack P  ) ) 
     [2] => Array ( [0] => Array ( [SSN] => 999338999 [borrowerName] => Angel M  ) ) 
) 

I want to be able to search for an SSN and get back the index of where it is

for example search($array,'SSN','412516789') should return 1


Of course you need a foreach. Can't do otherwise.

function search($array,$key,$value) {
  foreach($array as $k=>$v) {
    if ($v[0][$key]==$value)
      return $k;
  }
}


Here's some function searching recursively in a multidim array, you may want to adapt it to your needs.

function findInMultiDim($array, $value){
    if(in_array($value,$array)) return TRUE;
    foreach($array as $v){
        if(is_array($v)){
            $result = findInMultiDim($v, $value);
            if($result === TRUE){
                return TRUE;
            }
        }
    }
    return FALSE;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜