PHPEveryday.com PHP and Web Development Tutorial
What are you looking for?


PHP Array: Searching Nested Array


PHP Array Tips - Part 19: Do you want to search a series of nested arrays for a particular key or value? use like this:
<?php

function arrayRecursiveSearch($needle, $haystack, $path=""){
  if(!is_array($haystack)){
    die("second argument is not array");
  }
  
  global $matches;
  
  foreach($haystack as $key=>$value)
  {
    if(preg_match("/$needle/i", $key)){
	  $matches[] = array($path . "$key/", "KEY: $key");
	}
	
	if(is_array($value)){
	  $path .= "$key/";
	  arrayRecursiveSearch($needle, $value, $path);
	  unset($path);
	}else{
	  if(preg_match("/$needle/i", $value)){
	    $matches[] = array($path . "$key/", "VALUE: $value");
	  }
	}
  }
  
  return $matches;
}

$arr = array("Asia"=>array('rambutan','duku'),
              "Australia"=>array('pear','kiwi'),
			  "Arab"=>array('kurma'));

print_r(arrayRecursiveSearch("ra",$arr));
// result: Array ( [0] => Array ( [0] => Asia/0/ [1] => VALUE: rambutan ) [1] => Array ( [0] => Australia/ [1] => KEY: Australia ) [2] => Array ( [0] => Arab/ [1] => KEY: Arab ) )
?>

Back to TOC Array Tips



Series this article:
PHP Array: Processing Array Use SPL
PHP Array: Processing Nested Array
PHP Array: Using SPL to Process Nested Array
PHP Array: Counting Number of Elements
PHP Array: Converting Strings to Arrays
PHP Array: Converting Arrays to Strings
PHP Array: Swapping Array keys and Values
PHP Array: Adding element to an Array
PHP Array: Taking an Element off the beginning of the array
PHP Array: Adding an Element to the Beginning of the Array
PHP Array: Taking an element off the end of the array
PHP Array: Adding or removing elements from the middle of an array
PHP Array: Extracting Contiguous Segments of an Array
PHP Array: Removing Duplicate Array Elements
PHP Array: Re-indexing Array
PHP Array: Randomizing Array
PHP Array: Searching Arrays
PHP Array: Searching Nested Array
PHP Array: Reversing Arrays
PHP Array: Filtering Array Elements
PHP Array: Sorting Arrays


Tag: array Category: PHP Basic Post : March 26th 2008 Read: 2,726 Bookmark and Share

blog comments powered by Disqus


Database Tutorial
  • Learn PHP MySQL
  • Learn PHP ADOdb
  • Learn PHP Data Object/PDO
  • Learn PHP XML
  • Learn PHP SimpleXML
Security Tutorial
  • Learn PHP Security
  • Learn HTTP Authentication
  • Learn PHPSecureSite
Framework Tutorial
  • Learn CodeIgniter
  • Learn Joomla
  • Learn Smarty
  • Learn Zend Framework
Template Tutorial
  • Learn Joomla Template
  • Learn WordPress Template
API Tutorial
  • Learn Facebook
JS Framework Tutorial
  • Learn MooTools
  • Learn JQuery
AJAX Tutorial
  • Learn AJAX in 10 Minutes
  • Learn AJAX Client Side
  • Learn AJAX PHP
  • Learn AJAX Remote Server
  • Learn AJAX Repetitive
  • Learn AJAX MySQL
  • Learn AJAX Grid
Web Services Tutorial
  • Learn Web Services NuSOAP
  • Learn Web Services WSDL
  • Learn Web Services WSDL Array
  • Learn Web Services .NET Grid
  • Learn Web Services WDDX
Package Post
  • Joomla Intro
  • Joomla Component
  • Joomla Module
  • Joomla MVC
  • Joomla MVC Backend
  • PostNuke Intro
  • Zend Framework Intro
  • Zend Framework Action
  • Zend Framework Database
  • Zend Framework Registry
  • Zend Framework Config
  • Zend Framework Login
  • Zend Framework Session
  • PHP Array Tips
  • PHP File Tips
  • PHP Email
  • PHP Ms Excel
  • PHP Pattern
  • PHP SMS
  • Flash Database
  • PHP Multitier
  • jQuery Introduction
  • jQuery Selectors
  • Portable Web Server
  • Web Mobile Intro
  • Drupal Installation
  • Drupal Configuration