1

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

3
  • 3
    What is the point of the multidimensional array here? Your data could easily be expressed w/o the second array. Commented Jun 7, 2011 at 17:07
  • Just foreach() the parent array and rummage around for what you want. php's array search functions aren't built for multidimensional searching. Commented Jun 7, 2011 at 17:10
  • Changing the structure is not exactly an option so I will have to work with this. foreach would be very inefficient Commented Jun 7, 2011 at 17:22

2 Answers 2

2

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;
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.