0

I am trying to write a piece of code that searches one column of 2-D array values and returns the key when it finds it. Right now I have two functions, one to find a value and return a boolean true or false and another (not working) to return the key. I would like to merge the two in the sense of preserving the recursive nature of the finding function but returning a key. I cannot think how to do both in one function, but working key finder would be much appreciated.

Thanks

function in_array_r($needle, $haystack, $strict = true) {
foreach ($haystack as $item) {
    if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
        return true;
    }
}

return false;

}

function loopAndFind($array, $index, $search){
     $returnArray = array();
     foreach($array as $k=>$v){
           if($v[$index] == $search){   
                $returnArray[] = $k;
           }
     }
     return $returnArray;

}`

Sorry, I meant to add an example. For instance:

Array [0]{
[0]=hello
[1]=6
}
[1]
{
[0]=world
[1]=4
}

and I want to search the array by the [x][0] index to check each string of words for the search term. If found, it should give back the index/key in the main array like "world" returns 1

3
  • Could you post an example array highlighting what you would like to match within it? "one column of 2-D array values and returns the key" is not very clear. Commented Apr 16, 2012 at 18:46
  • why are you using index in second function? Commented Apr 16, 2012 at 18:52
  • I posted an example above and I am not really sure why I am using index. I think it was supposed to be the index of the second array like Array[0]{[index]=stuff, [1]=words} etc Commented Apr 16, 2012 at 19:46

2 Answers 2

2

This works:

$array = array(array('hello', 6), array('world', 4));
$searchTerm = 'world';

foreach ($array as $childKey => $childArray) {
    if ($childArray['0'] == $searchTerm) {
        echo $childKey; //Your Result
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

You already have all you need in your first function. PHP does the rest:

$findings = array_map('in_array_r', $haystack);
$findings = array_filter($findings); # remove all not found
var_dump(array_keys($findings)); # the keys you look for

2 Comments

I am sorry, I am not sure I know what you are trying to do. I need to search through the array one term at a time rather than run the function all at once. Does that make sense or did I misunderstand your code? Thanks
It was not really clear from your question what you want to search and how. You have dropped some functions w/o showing any code how you want to call them, what the input is and what the output should be. If you add that, I'm pretty sure I can make the answer equally clear.

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.