Page 1 of 1

How can i search element in multidimentional array

Posted: Mon Jul 31, 2006 3:07 am
by madhavvyas
Pimptastic | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hello friend

I have one problem

i got one array

Code: Select all

$md={
[0]=>array{
            [0]=>1
                  }
}


How can i check 1 is there or not :?:

Thank you


Pimptastic | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Piece of code

Posted: Mon Jul 31, 2006 5:36 am
by ronverdonk
I found this little piece of code in the comments of the PHP document. It returns you either the path or FALSE.

Code: Select all

/**
 * Searches haystack for needle and returns an array of the key path if it is found in the (multidimensional) array, FALSE otherwise. 
*/

function array_searchRecursive( $needle, $haystack, $strict=false, $path=array() )
{
   if( !is_array($haystack) ) {
       return false;
   }
   foreach( $haystack as $key => $val ) {
       if( is_array($val) && $subPath = array_searchRecursive($needle, $val, $strict, $path) ) {
           $path = array_merge($path, array($key), $subPath);
           return $path;
       } elseif( (!$strict && $val == $needle) || ($strict && $val === $needle) ) {
           $path[] = $key;
           return $path;
       }
   }
   return false; 
}

Posted: Mon Jul 31, 2006 8:42 am
by madhavvyas
Thanks

but it is not working for me

I tried with following data

Code: Select all

$md=array(array("S1",array("S3")),"S2");
it display

Array ( [0] => 0 [1] => 0 )


what it means.................

Posted: Mon Jul 31, 2006 8:46 am
by RobertGonzalez
You're going to have to go deeper, until you get to the point where the array dimension you are in is a single dimension array, then you can use in_array().