How can i search element in multidimentional array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
madhavvyas
Forum Newbie
Posts: 9
Joined: Wed Jul 26, 2006 7:10 am

How can i search element in multidimentional array

Post 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]
User avatar
ronverdonk
Forum Commoner
Posts: 34
Joined: Sat Jun 10, 2006 7:06 am
Location: Netherlands

Piece of code

Post 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; 
}
madhavvyas
Forum Newbie
Posts: 9
Joined: Wed Jul 26, 2006 7:10 am

Post 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.................
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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().
Post Reply