Jump to content

Associative array access


Bill_Lea

Recommended Posts

I am trying to simplify my code by moving isome of it to a function.  I need to pass this func tion an associative array which consistw of filename +> file uri.

I am atempting to build this array by using the statemet $file2uri[$filename]=$uri;

This seems to work I can dump it in DRUPAL's DVM and it looks ok.

I'm trying to pass by reference so  I define the function as function fixline(&$line,&$file2uri);

inside the function DVM shows the array.

$line contains the filename and I parse it out. It looks ok.

However when I try to access the array nothing comes back.

here is some debug output that is generated:

Fixline entered

MEDIA detected in MEDIA="vwCar01.jpg"

Filename resolved to:vwCar01jpg

array( 'vwcar01jpg' => 'public://zifimage/Images/13/41/vwcar01.jpg', 'vwcar02jpg' => 'public://zifimage/Images/13/42/vwcar02.jpg', 'vwcar03jpg' => 'public://zifimage/Images/13/43/vwcar03.jpg', 'vwcar04jpg' => 'public://zifimage/Images/13/44/vwcar04.jpg', 'vwcar05jpg' => 'public://zifimage/Images/13/45/vwcar05.jpg', 'vwcar06jpg' => 'public://zifimage/Images/13/46/vwcar06.jpg', 'vwcar07jpg' => 'public://zifimage/Images/13/47/vwcar07.jpg', 'vwcar08jpg' => 'public://zifimage/Images/13/48/vwcar08.jpg', 'vwcar09jpg' => 'public://zifimage/Images/13/49/vwcar09.jpg', 'vwcar10jpg' => 'public://zifimage/Images/13/50/vwcar10.jpg', 'vwcar11jpg' => 'public://zifimage/Images/13/51/vwcar11.jpg', 'vwcar12jpg' => 'public://zifimage/Images/13/52/vwcar12.jpg', 'vwcar13jpg' => 'public://zifimage/Images/13/53/vwcar13.jpg', 'vwcar14jpg' => 'public://zifimage/Images/13/54/vwcar14.jpg', '' => '', )

file_uri was

line set to: MEDIA=""

FIX Line Returned

this is generated by:

 

function fixline(&$line,&$file2uri){
    dvm($file2uri);
  drupal_set_message("Fixline entered",'status');
  switch($line){
    case strpos($line,'MEDIA')!== FALSE:

      drupal_set_message("MEDIA detected in $line",'status');
      $filename=str_replace('MEDIA=','',$line);
      $filename=str_replace('"','',$filename);
      $filename=str_replace('.','',$filename);
      $filename=rtrim($filename);
      $filename=trim($filename);
      drupal_set_message("Filename resolved to: $filename",'status');
       $file_uri=$file2uri[$filename];
      drupal_set_message('File2uri['.$filename.'] was'. $file2uri[$filename],'status');
      drupal_set_message("file_uri was$file_uri",'status');
      $line = '      MEDIA="' . $file_uri .'"';
      drupal_set_message("line set to:$line",'status');
    break;
    
    case strpos('AUDIO=',$line) !== FALSE:
      drupal_set_message('The substring AUDIO exists in given string.','status');
       $fn=str_replace('AUDIO="','',$line );
       $fn=str_replace('"','',$fn);    
       $fn=rtrim($fn);
       $fn=trim($fn);
       $file_uri=$file2uri[$filename];
      $line = '      AUDIO="' . $file_uri .'"'."\n";
    break;
    
    case strpos('ANNOTATIONS=',$line) !== FALSE:
      drupal_set_message('The substring ANNOTATIONS exists in given string.','status');
       $fn=str_replace('ANNOTATIONS="','',$line );
       $fn=str_replace('"','',$fn);    
       $fn=rtrim($fn);
       $fn=trim($fn);
       $file_uri=$file2uri[$filename];
      $line = '      ANNOTATIONS="' . $file_uri .'"'."\n";
    break;

    case strpos("TRACKING=",$line)!==FALSE:
      drupal_set_message('The substring TRACKING exists in given string.','status');
      $fn=str_replace('TRACKING="','',$line );
       $fn=str_replace('"','',$fn);    
       $fn=rtrim($fn);
       $fn=trim($fn);
       $file_uri=$file2uri[$filename];
       $line = '      TRACKING="' . $file_uri .'"'."\n";
    break;

    case strpos("HOTASPOTPATH=",$line)!==FALSE:
      $fn=str_replace('HOTSPOTPATH="','',$line );
      $fn=str_replace('"','',$fn);           $fn=rtrim($fn);
      $fn=trim($fn);
      $file_uri=$file2uri[$filename];
      $line = '      HOTSPOTPATH="' . $file_uri .'"'."\n";
    break;
    
    case  strpos("IMAGELIST=",$line)!==FALSE:
      $fn=str_replace('IMAGELIST="','',$line );
      $fn=str_replace('"','',$fn);           $fn=rtrim($fn);
      $fn=trim($fn);
      $file_uri=$file2uri[$filename];
      $line = '      IMAGELIST="' . $file_uri .'"'."\n";
    break;
    
    case strpos("HOTSPOT=",$line)!==FALSE:
      $fn=str_replace('HOTSPOT="','',$line );
      $fn=str_replace('"','',$fn);           $fn=rtrim($fn);
      $fn=trim($fn);
      $file_uri=$file2uri[$filename];
      $line = '      HOTSPOT="' . $file_uri .'"'."\n";
    break;
    
    case strpos("IMAGEPATH=",$line)!==FALSE:
      $fn=str_replace('IMAAGEPATH="','',$line );
      $fn=str_replace('"','',$fn);           $fn=rtrim($fn);
      $fn=trim($fn);
      $file_uri=$file2uri[$filename];
      $line = '      IMAGEPATH="' . $file_uri .'"'."\n";
    break;
    case strpos("HOTSPOTXMLPATH=",$line)!==FALSE:
      $fn=str_replace('IMAAGEPATH="','',$line );
      $fn=str_replace('"','',$fn);           $fn=rtrim($fn);
      $fn=trim($fn);
      $file_uri=$file2uri[$filename];
      $line = '      IMAGEPATH="' . $file_uri .'"'."\n";
    break;
    
    case strpos("ANNOTATIONXMLPATH=",$line)!==FALSE:
      $fn=str_replace('ANNOTATIONXMLPATH="','',$line );
      $fn=str_replace('"','',$fn);           $fn=rtrim($fn);
      $fn=trim($fn);
      $file_uri=$file2uri[$filename];
      $line = '      ANNOTATIONXMLPATH="' . $file_uri .'"'."\n";
    break;
    
    case strpos("SLIDEXMLPATH=",$line)!==FALSE:
      $fn=str_replace('SLIDEXMLPATH="','',$line );
      $fn=str_replace('"','',$fn);           $fn=rtrim($fn);
      $fn=trim($fn);
      $file_uri=$file2uri[$filename];
      $line = '      ANNOTATIONXMLPATH="' . $file_uri .'"'."\n";
    break;

    default:
    Drupal_set_message("Fixline found nothing to update",'status');
    break;
  }//end Switch
  Drupal_set_message("FIX Line Returned",'status');
}//end function Fixline

Something is strane here. I should be able to build and array and pass it by reference to a function and then access it by a calculated string key.  Can you suggest what I am doing wrong? I'm baffeled. and Google is no help.

 

 

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.