tags:

views:

30

answers:

1

I'm trying to find the index based on the value stored within it.

This would normally be easy, but the array I'm working with is highly nested. Each index 0,1,2 has fields f1,f2,f3 . I'm trying to find which index 0,1,2 has in its f2 field the value this stored in it. In this case, it's index 0. So that's the output I'm looking for. Is there a neat trick in PHP to do this effectively?

$somearray[0][f1] = "not this";
$somearray[0][f2] = "this"; 
$somearray[0][f3] = "not this"; 

$somearray[1][f1] = "not this";
$somearray[1][f2] = "not this";
$somearray[1][f3] = "not this"; 

$somearray[2][f1] = "not this"; 
$somearray[2][f2] = "not this"; 
$somearray[2][f3] = "not this"; 
+3  A: 

In this case, it's index 0. So that's the output I'm looking for.

$somearray[0]['f1'] = "not this";
$somearray[0]['f2'] = "this";
$somearray[0]['f3'] = "not this";

$somearray[1]['f1'] = "not this";
$somearray[1]['f2'] = "not this";
$somearray[1]['f3'] = "not this";

$somearray[2]['f1'] = "not this";
$somearray[2]['f2'] = "not this";
$somearray[2]['f3'] = "not this";

foreach($somearray as $key => $value)
{
  if($value['f2'] === 'this')
  {
     echo $key; // find out the key
  }
}

Output:

0
Sarfraz
Thank you. Does what I was trying to do.
lok
@lok: You are welcome :)
Sarfraz