Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. Linux, Apache, MySQL, PHP
  4. recursive array search

recursive array search

Scheduled Pinned Locked Moved Linux, Apache, MySQL, PHP
databasedata-structureshelp
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nhsal69
    wrote on last edited by
    #1

    Hi there, I have a piece of code to search through a multidimensional array and return the keys of successful hits... The problem is that I can see it has checked all though the array (the echo "@"; shows an @ for each element of the array) but the matches are not returned, the array contains at least 30 "status" fields. Can anyone tell me where I'm going wrong.. Thanks Here is the code:

    $haystack = $server1 = $tx->extractTable();
    $needle = 'Status';
    $index = null;

    $aIt = new RecursiveArrayIterator($haystack);
    $it = new RecursiveIteratorIterator($aIt);

    while($it->valid())
    {       
        if (((isset($index) AND ($it->key() == $index)) OR (!isset($index))) AND ($it->current() == $needle)) {
        	
    	return $aIt->key();
    	
    	echo $aIt->key();
    
        }
    
    echo "@";
       
        $it->next();
    
    }
    
    
    
    return false;
    
    F 1 Reply Last reply
    0
    • N nhsal69

      Hi there, I have a piece of code to search through a multidimensional array and return the keys of successful hits... The problem is that I can see it has checked all though the array (the echo "@"; shows an @ for each element of the array) but the matches are not returned, the array contains at least 30 "status" fields. Can anyone tell me where I'm going wrong.. Thanks Here is the code:

      $haystack = $server1 = $tx->extractTable();
      $needle = 'Status';
      $index = null;

      $aIt = new RecursiveArrayIterator($haystack);
      $it = new RecursiveIteratorIterator($aIt);

      while($it->valid())
      {       
          if (((isset($index) AND ($it->key() == $index)) OR (!isset($index))) AND ($it->current() == $needle)) {
          	
      	return $aIt->key();
      	
      	echo $aIt->key();
      
          }
      
      echo "@";
         
          $it->next();
      
      }
      
      
      
      return false;
      
      F Offline
      F Offline
      fly904
      wrote on last edited by
      #2

      nhsal69 wrote:

      return $aIt->key();

      echo $aIt->key();

      Is there any reason why you are returning and then outputing? The output code (echo) is unreachable.

      If at first you don't succeed, you're not Chuck Norris.

      N 1 Reply Last reply
      0
      • F fly904

        nhsal69 wrote:

        return $aIt->key();

        echo $aIt->key();

        Is there any reason why you are returning and then outputing? The output code (echo) is unreachable.

        If at first you don't succeed, you're not Chuck Norris.

        N Offline
        N Offline
        nhsal69
        wrote on last edited by
        #3

        Nope I just wanted to see what if anything was getting returned.. but it appears not.

        F 1 Reply Last reply
        0
        • N nhsal69

          Nope I just wanted to see what if anything was getting returned.. but it appears not.

          F Offline
          F Offline
          fly904
          wrote on last edited by
          #4

          Then there would appear to be something wrong with your logic.

          nhsal69 wrote:

          if (((isset($index) AND ($it->key() == $index)) OR (!isset($index))) AND ($it->current() == $needle))

          If at first you don't succeed, you're not Chuck Norris.

          N 1 Reply Last reply
          0
          • F fly904

            Then there would appear to be something wrong with your logic.

            nhsal69 wrote:

            if (((isset($index) AND ($it->key() == $index)) OR (!isset($index))) AND ($it->current() == $needle))

            If at first you don't succeed, you're not Chuck Norris.

            N Offline
            N Offline
            nhsal69
            wrote on last edited by
            #5

            Ok, I've been staring at that for ages and my logic has vanished... I re wrote it to this the below.... Only problem is that I still don't get a valid response.. the array is of the form : [1] => Array ( [Name ] => Aberdeen [Status ] => Ready [Location ] => [Jobs ] => 0 [Model ] => HPUniversalPrintingPCL6 [Comment ] => PRINT_EARL The correct number of "@" are retuned so it's performing the correct number of itterations, but no giving me the results... I should have mentioned that I'm completely new to PHP and am kinda winging it.... Start of code chunk $it = new RecursiveIteratorIterator( new RecursiveArrayIterator($server1)); echo ""; foreach ($it as $key=>$val) { echo "@"; if ($val == "PRINT_EARL") { echo ""; echo ""; echo ""; echo ""; } Else echo $key.":".$val."\n"; } End of code chunk -- Modified Monday, May 3, 2010 10:51 AM

            "; echo $key; echo "

            "; echo $val; echo "

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups