Lost elements in multidimensional 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
craigm
Forum Newbie
Posts: 2
Joined: Thu Sep 01, 2005 6:18 pm

Lost elements in multidimensional array

Post by craigm »

I'm having some trouble passing & returning multi-dimensional arrays using php4. It seems I lose the contents of the lower dimensional arrays.

My code is:

Code: Select all

<?php 
 // LOGIC: 
  
 // constants: 
  define(HREF, "href"); 
  define(META, "meta"); 
  
 // variables: 
  $files = array(); 

 // 'private' functions: 

  /* 
   * retrieve names of all files within $basedir (not incl. sub dirs). 
   * return as an array of strings 
   */ 
  function loadFileArray ( $basedir ) { 
     $filearray = array(); 
     if ( is_dir( $basedir ) ) { 
      if ( $dhandle=opendir( $basedir ) ) { 
        while ( ( $file=readdir( $dhandle ) ) !== false) { 
          $tmp = array(); 
          $tmp[HREF] = $file; 
          $filearray[] = $tmp; 
        } 
        closedir($dhandle); 
      } 
    } 
     return $filearray; 
  } 
  
  /* 
   * scan all filenames in $filearray testing whether the name 
   * matches the regular expression $filterexpr. 
   * if $match=true return an array of matching files, 
   * otherwise return an array of unmatching files. 
   */ 
  function filterFilesByName ( $filearray, $filterexpr, $match ) { 
   // init: 
    $matchfiles = array(); 
    $unmatchfiles = array(); 
   // parse: 
    for ( $i=0; $i < count( $filearray ); $i++) { 
       if ( preg_match( $filterexpr, $filearray[$i][HREF] ) ) { 
        $matchfiles[] = $filearray[$i]; 
       } else { 
         $unmatchfiles[] = $filearray[$i]; 
       } 
    } 
   // return: 
    if ( $match ) 
      return $matchfiles; 
    else 
      return $unmatchfiles; 
  } 
  
  /* 
   * load all meta-tags' contents into associative array of 
   * integer indexed arrays: 
   * 
   * results = { "meta-name1" -> {0->"content1", 1->"content2"}, 
   *             "meta-name2" -> {0->"content1", 1->"content2"}, 
   *             "meta-name3" -> {0->"content1", 1->"content2"} 
   *           } 
   */ 
  function loadMetaArray ( $href ) { 
   // init: 
    $meta = array(); 
   // parse: 
    $tmp = get_meta_tags( $href ); 
    while( list( $name, $content ) = each( $tmp ) ) { 
      $meta[$name] = array(); 
      $meta[$name] = explode( ", ", $content ); 
    } 
   // return results: 
    return $meta; 
  } 
  
  
  /* 
   * scan all filenames in $filearray testing whether their 
   * meta-content contains $filterexpr. 
   * if $match=true return an array of matching files, 
   * otherwise return an array of unmatching files. 
   */ 
  function filterFilesByContent ( $filearray, $filterexpr, $match ) { 
   // init: 
    $matchfiles = array(); 
    $unmatchfiles = array(); 
   // parse: 
    for ( $i=0; $i < count( $filearray ); $i++) { 
      $isMatch = false; // reset flag 
      while( list( $name, $content ) = each( $filearray[$i][META] ) ) { 
         for ( $i2=0; $i2 < count( $content ); $i2++) { 
         if ( preg_match( $filterexpr, $content[$i2] ) ) 
            $isMatch = true; // flag file as matching filter 
         } 
      } 
       if ( $isMatch ) { 
        $matchfiles[] = $filearray[$i]; 
       } else { 
         $unmatchfiles[] = $filearray[$i]; 
       } 
    } 
   // return: 
    if ( $match ) 
      return $matchfiles; 
    else 
      return $unmatchfiles; 
  }
?> 

<html> 
  <head></head> 
  <body> 

<?php 
 // PRESENTATION: 
  
 // obtain an array of all files in current dir: 
  $files = loadFileArray ( getcwd() ); 

 // filter files by filename:  
  $files = filterFilesByName( $files, "/.*\.htm/", true); 
  $files = filterFilesByName( $files, "/\bcs-.*/", false); 
  $files = filterFilesByName( $files, "/\btx-.*/", false); 

 // load meta data as additional 'field' to file 'record': 
  for ($i=0; $i<count($files); $i++) 
  { 
    $files[$i][META] = loadMetaArray ( $files[$i][HREF] ); 
  } 

 // filter files by meta-content: 
  $files = filterFilesByContent( $files, "/sport/", true ); 

 // echo to screen: 
  for ( $i=0; $i < count( $files ); $i++) { 
     echo $files[$i][HREF]." contains:<br />"; 
    while( list( $name, $content ) = each( $files[$i][META] ) ) { 
      echo $name.": "; 
        for ( $i2=0; $i2 < count( $content ); $i2++) { 
        echo $content[$i2]." "; 
      } 
      echo "<br />"; 
    } 
  } 
  echo "<br />"; 
  echo "<br />"; 
  
?> 

  </body> 
</html>

after calling filterFilesByContent( $files, "/sport/", true ); I lose the contents of $files[$i][META]. Am I making by value / by reference errors?

I'm kind of new to php so is this even the best way to structure the file related data?

please help.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Code: Select all

define(HREF, "href");
  define(META, "meta"); 
// should be
  define('HREF', "href");
  define('META', "meta");
You get errors but those are likely 'hidden' as the error levels settings in the php.ini is set to low.

Code: Select all

Notice:  Use of undefined constant HREF - assumed 'HREF' in \test\index.php on line 6

Notice:  Use of undefined constant META - assumed 'META' in \test\index.php on line 7
craigm
Forum Newbie
Posts: 2
Joined: Thu Sep 01, 2005 6:18 pm

Post by craigm »

Thanks for the feedback. I'm on holiday for 2 weeks now so I'll have to take a look after the 18th.
Post Reply