WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Array Searching Recursively
Categories : PHP, Arrays Click here to Update Your Picture
Joseph Crawford
Date : May 28th 2004
Grade : 3 of 5 (graded 3 times)
Viewed : 5900
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Joseph Crawford
Action : Grade This Code Example
Tools : My Examples List

 
Like this code?
Show the author your appreciation.
Submit your own code examples 
 

The following code will probably be understood better by advanced user, this code will only search array elements.

meaning if you have $array[books][php][0] and you are searching for 'php' it will not find the [php] array, i am sure that with minor
modifications you could make it find either the element or an array element.

Hopefully this code will help someone else, i just spent 30 minutes getting this to work ;)


<?php

// the function that will recursivly search an array.
function searchArrayRecursive($needle, $haystack){

 
// loop through the haystack that has been passed in
 
foreach ($haystack as $key => $arr) {

   
// check to make sure that the element is an array
   
if(is_array($arr)) {

     
/* this is the tricky line, this will take the value
         or $arr and call the function again each time
         the function is called, $ret is set with the
         return value of the function call, this builds
         the string that get's returned.
      */
     
$ret=searchArrayRecursive($needle, $arr);

     
/* check to make sure that the function call did
         not return -1 and return the value of the $key and
         the $ret
      */
     
if($ret!=-1) return $key.','.$ret;

    } else {

     
/* check the array element and see if it matches the
         search term. if it does, return the $key of the
         element.
      */
     
if($arr == $needle) return (string)$key;

    }

  }

 
// nothing was found, return -1

 
return -1;

}



// create the actual array to use ;)
$myArray = array (
   
"computer"  => array(
     
"hardware" => array(
         
"modem", "hard drive", "ram", "mother board"
     
),
     
"softare" => array(
         
"photoshop", "quickbooks"
     
)
    ),
   
"cars" => array(
     
"honda" => array(
         
"civic", "accord"
     
),
     
"toyota" => array(
         
"4Runner", "camry", "carolla", "rav4", "tundra"
     
)
   ),
   
"books" => array(
     
"php" => array(
         
"sams teach yourself php mysql and apache",
         
"core php programming"
     
)
   )
);

// nicely format and print the value of $myArray
print "<pre>";
print_r($myArray);
print
"</pre>";


// set the value to find in the array
$findWhat = "tundra";

// call the function with the needle and haystack.
$result = searchArrayRecursive($findWhat, $myArray);

// check to make sure the returned result was not -1
if($result != -1) {

 
/* create the result array from the string returned from
     the function.
  */
 
$result = explode(',', $result);

 
/* Loop through the array to create an array format string
     such as $array[this][that][0]
  */
 
foreach($result as $element) {

   
// append the element to the $result string
   
$result .= '['.$element.']';

  }

}

// check to make sure the result of the function is not -1
if($result != -1) {

 
// tell us we found what we were looking for.
 
echo 'Found '.$findWhat.'! '.$result;

} else {

 
/* tell us that we didnt find any array keys matching
     the search term.
  */
 
echo 'Couldn\'t Find <b>\''.$findWhat.'\'</b> in the array.';

}
?>



PHP Script to find url links in a page
Categories : PHP, URLs, Regexps, Arrays
Tag content retrieval from websites with preg_match
Categories : PHP, Regexps, Arrays, HTML and PHP
Array values from javascript to php
Categories : PHP, Java Script, Arrays
clearing variables in php3
Categories : Variables, Arrays, PHP
Weighted Random - Random Scripts usually chose one out of each item, and each item have an equal chance to be chosen. But what if you want an item to be chosed more frequently than other?
Categories : PHP, Math., Arrays
Display list of files within current and subdirectories (recursively) showing each file as an anchored link and each directory as a category header.
Categories : Filesystem, Directories, Arrays, PHP
Array Insertion
Categories : PHP, PHP Classes, Arrays
Check for functional file links (broken Files)
Categories : PHP, Data Validation, FTP, Regexps, Arrays
Single-file PHP news system with automatic folder structure creation
Categories : PHP, Filesystem, Arrays
This gets the http response headers for a given url and returns them in an assoc array. i.e. to test if a url exists: $array = get_http_headers($url); if($array[result]=200) { }
Categories : HTTP, Arrays, PHP
Beginners Array Functions
Categories : PHP, Beginner Guides, Arrays
How to pass an array from one PHP Script to another via an HTML form
Categories : PHP, HTML and PHP, Arrays
How to load a query result into a PHP Array
Categories : PHP, Databases, Arrays, MySQL
dynamic table columns
Categories : PHP, HTML and PHP, Arrays, Databases, MySQL
A recursive function to traverse a multi-dimensional array where the dimensions are not known
Categories : Arrays, PHP, Algorithms