Web Analytics Made Easy -
StatCounter Array Processing - CodingForum

Announcement

Collapse
No announcement yet.

Array Processing

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Array Processing

    I have a changeable list of files that need to be sent as email attachments. My cgi app can process and assemble the list then pass it on to PHP.

    I'm running into trouble when trying to put the files and their paths into an array.

    This works:
    PHP Code:
    $path '/Library/WebServer/Documents/TheSite/TheFiles';
    $files = array($path.'/ThisFile.pdf',$path.'/ThatFile.pdf'); 
    And this works:
    PHP Code:
    $files = array('/Library/WebServer/Documents/TheSite/TheFiles/ThisFile.pdf','/Library/WebServer/Documents/TheSite/TheFiles/ThatFile.pdf'); 
    So this should work, but it fails to find the files:
    PHP Code:
    $filelist '/Library/WebServer/Documents/TheSite/TheFiles/ThisFile.pdf','/Library/WebServer/Documents/TheSite/TheFiles/ThatFile.pdf';
    $files = array($filelist); 
    I've tried other variations but can't find a way to handle it.

    The need is to find a format with the path and file name that I can pass to PHP as I've done in $filelist above. I can't see any other way to provide an array of file names and combine them with the path to make the equivalent of $path.'/ThisFile.pdf',$path.'/ThatFile.pdf'

  • #2
    $a | $b <> $c.

    Don't know why you think that last line should work. That would be a syntax error, since you cannot just place a , in the middle of nowhere, and in the event that its a correct string you couldn't just wrap it in an array. You could explode it then though.

    I don't quite follow what your question here is. Are you asking how to prepend the same string to each of the items within an array?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

    Comment


    • #3
      In my cgi app, I have a list, an array, of several file names such as This.pdf, That.pdf, etc.. I know where they are so I have the path. I can pass the list of files as is to PHP separately or I can add the path to each file name.

      It seems that putting them together would be best before I pass it to PHP., so each would become /Library/WebServer/Documents/TheSite/TheFiles/ThisFile.pdf

      In my cgi I can very easily format it with quotes, commas or whatever, but I just can't find a way to get the same end result in the PHP process.

      So I'm looking for the syntax that would make this list:
      /Library/WebServer/Documents/TheSite/TheFiles/ThisFile.pdf
      /Library/WebServer/Documents/TheSite/TheFiles/ThatFile.pdf

      become the same as $files after being run through this:
      PHP Code:
      $path '/Library/WebServer/Documents/TheSite/TheFiles'
      $files = array($path.'/ThisFile.pdf',$path.'/ThatFile.pdf'); 
      Last edited by MichigamJim; Aug 29, 2011, 09:32 AM.

      Comment


      • #4
        You could use a "for" loop or a "foreach" loop for this:

        For loop:
        PHP Code:
        $path="/Library/WebServer/Documents/TheSite/TheFiles'/";
        for(
        $i=0;$i<count($files);$i++){
            
        $files[$i]=$path.$files[$i];

        Foreach loop:
        PHP Code:
        $path="/Library/WebServer/Documents/TheSite/TheFiles'/";
        foreach(
        $files as $key => $value){
            
        $value=$path.$value;

        The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
        See Mediocrity in its Infancy
        It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
        Seek and you shall find... basically:
        validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

        Comment


        • #5
          Loops would work, but it just feels clumsy. I'm sure there's some simple syntax I'm missing that would make my array of paths and file names come out the same as array($path.'/ThisFile.pdf',$path.'/ThatFile.pdf')

          Comment


          • #6
            Originally posted by MichigamJim View Post
            Loops would work, but it just feels clumsy. I'm sure there's some simple syntax I'm missing that would make my array of paths and file names come out the same as array($path.'/ThisFile.pdf',$path.'/ThatFile.pdf')
            You mean just the printing of them? var_export($path); would literally show it as if you were to make an array of them in PHP.
            ie:
            PHP Code:
            <?php
            $files 
            = array('/Library/WebServer/Documents/TheSite/TheFiles/ThisFile.pdf','/Library/WebServer/Documents/TheSite/TheFiles/ThatFile.pdf');  

            var_export($files);
            ?>
            Would literally produce:
            Code:
            array ( 0 => '/Library/WebServer/Documents/TheSite/TheFiles/ThisFile.pdf', 1 => '/Library/WebServer/Documents/TheSite/TheFiles/ThatFile.pdf', )
            Or is it something else you are referring to?

            Or are you asking something like a directory listing which can be done using a glob. Still need to either loop it or implode at as these are array types, and if you do a scan technique than its a loop on the resource.
            PHP Code:
            header('HTTP/1.1 420 Enhance Your Calm'); 
            Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

            Comment


            • #7
              explode(

              That is the simple solution I was looking for. It converts my list to an array.

              Comment

              Working...
              X