0

I have a multi-dimensional array (see below) and I want to extract the key of a certain value.

For instance:

  • Give me the key of "Value 2". This should return 1.
  • Give me the key of "Column 3". This should return 2.

Array ( [0] => Array ( [0] => Column 1 [1] => Column 2 [2] => Column 3 ) 
        [1] => Array ( [0] => Value 1 [1] => Value 2 [2] => Value 3 )

Is there a simple function to make this work?

2
  • 1
    Use php.net/manual/en/function.array-search.php, but you either loop the array or use $array[0] etc... Commented May 26, 2017 at 19:50
  • Thanks for the quick reply. It works for me! Commented May 26, 2017 at 19:58

1 Answer 1

1
function findKey($array,$prop){
    foreach($array as $key => $val){
        foreach($array[$key] as $index => $value)
            if( $value === $prop ){
                return $index;
            }
    }   
}

findKey($array,'Value 2');
Sign up to request clarification or add additional context in comments.

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.