...

View Full Version : Array Processing



MichigamJim
08-29-2011, 04:44 AM
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:

$path = '/Library/WebServer/Documents/TheSite/TheFiles';
$files = array($path.'/ThisFile.pdf',$path.'/ThatFile.pdf');

And this works:

$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:

$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'

Fou-Lu
08-29-2011, 07:03 AM
$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?

MichigamJim
08-29-2011, 01:30 PM
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:

$path = '/Library/WebServer/Documents/TheSite/TheFiles';
$files = array($path.'/ThisFile.pdf',$path.'/ThatFile.pdf');

Rowsdower!
08-29-2011, 01:39 PM
You could use a "for" loop or a "foreach" loop for this:

For loop:

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

Foreach loop:

$path="/Library/WebServer/Documents/TheSite/TheFiles'/";
foreach($files as $key => $value){
$value=$path.$value;
}

MichigamJim
08-29-2011, 06:18 PM
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')

Fou-Lu
08-29-2011, 06:36 PM
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
$files = array('/Library/WebServer/Documents/TheSite/TheFiles/ThisFile.pdf','/Library/WebServer/Documents/TheSite/TheFiles/ThatFile.pdf');

var_export($files);
?>

Would literally produce:


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.

MichigamJim
08-30-2011, 04:48 PM
explode(

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum