Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

multidimensional array recursive search

Status
Not open for further replies.

monkle

Programmer
Feb 11, 2004
132
0
0
US
PHP Version 5.1.2-1.dotdeb.2

I am doing something right now that requires that I check for the existence of a value in a multidimensional array. It doesn't matter at what level in the multidimensional array the value is found, I just need to know if it's anywhere in the array or not.

I have been searching on this for a while, first in the pho.net documentation and then on Google, and while I find a plethora of information on searching arrays, I have had no success in dealing with multidimensional arrays. Any input would be greatly appreciated.
 
The trouble with looking for prebuilt code on searching multidimensional arrays is that the code must match the structure of the array. And the structures of multidimensional arrays can vary grandly.

What is the structure of your array?


Want the best answers? Ask the best questions! TANSTAAFL!
 
The structure varies.

I finally found something that helped, which led to the following user-defined function:

protected function inArrayR($strVal, $arrHeap){
foreach ($arrHeap as $value) {
if ($strVal == $value){
return true;
} elseif (is_array($value)) {
if ($this->inArrayR($strVal, $value)){
return true;
}
}
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top