Cep Posted November 7, 2007 Share Posted November 7, 2007 Hello! This is what I am trying to do: I have an array like this, array ( array($path, $ext, $file), array($path, $ext, $file), ..... ) I need to split $file to obtain its version number and file name so that I can eventually get the latest version of each file present in the array. So I can change this, $array = array( array("\\myplace\", "pdf", "davesfile~001.pdf"), array("\\myplace\", "pdf", "davesfile~002.pdf"), array("\\myplace\", "pdf", "davesfile~003.pdf"), array("\\myplace\", "pdf", "craigsfile~001.pdf"), array("\\myplace\", "pdf", "craigsfile~002.pdf"), array("\\myplace\", "pdf", "craigsfile~003.pdf"), array("\\myplace\", "pdf", "craigsfile~004.pdf"), array("\\myplace\", "pdf", "waynesfile~003.pdf") ); into this, $array = array( array("\\myplace\", "pdf", "davesfile~003.pdf"), array("\\myplace\", "pdf", "craigsfile~004.pdf"), array("\\myplace\", "pdf", "waynesfile~003.pdf") ); I have started writing a function to do this, it will take the file array as above and foreach sub array it will explode $file into its name and extension and then explode it again to split the name from the version. At this point I am at a stumbling block trying to figure out how to test the data and I keep getting array to string notices when using variable variables. Can anyone help me out? <?php function sort_files($filearray) { // Array = (array($path, $ext, $filename),array($path, $ext, $filename)); $myarray = array(); $varray = array(); foreach ($filearray as $fileinfo) { // Split name and extension $split_name = explode(".", $fileinfo[2]); // Split name and version $split_further = explode("~", $split_name[0]); // Assign filename and version $filename = $split_further[0]; $version = $split_further[1]; // Assign varray with the filename, eg. $$varray = "myfile"; $myfile = array(); $$varray = $filename; // Find is filename exists in new array if (in_array($filename, $myarray)===false) { $myarray[] = $filename; } else { $varray[] = $version; } } return $myarray; } $array = array( array("here", "ext", "davesfile~001.pdf"), array("here", "ext", "davesfile~002.pdf"), array("here", "ext", "davesfile~003.pdf"), array("here", "ext", "craigsfile~001.pdf"), array("here", "ext", "craigsfile~002.pdf"), array("here", "ext", "craigsfile~003.pdf"), array("here", "ext", "craigsfile~004.pdf"), array("here", "ext", "waynesfile~003.pdf") ); $newarray = sort_files($array); echo "<pre>"; var_dump($newarray); echo "</pre>"; ?> Quote Link to comment Share on other sites More sharing options...
Orio Posted November 7, 2007 Share Posted November 7, 2007 I lost you on this line: $$varray = $filename; $varray is an array, so how can you use it in variable-variables? Here's how I think it should be (I've tested it and the output was ok): <?php function sort_files($filearray) { $varray = array(); foreach ($filearray as $key => $fileinfo) { // Split name and extension $split_name = explode(".", $fileinfo[2]); // Split name and version $split_further = explode("~", $split_name[0]); // Assign filename and version $filename = $split_further[0]; $version = $split_further[1]; // Set in $varray[$filename] the number of the latest version if(!isset($varray[$filename][0]) || intval($version) > intval($varray[$filename][0])) $varray[$filename] = array($version, $key); } $myarray = array(); //Build the result foreach($varray as $data) $myarray[] = $filearray[$data[1]]; return $myarray; } $array = array( array("here", "ext", "davesfile~001.pdf"), array("here", "ext", "davesfile~002.pdf"), array("here", "ext", "davesfile~003.pdf"), array("here", "ext", "craigsfile~001.pdf"), array("here", "ext", "craigsfile~002.pdf"), array("here", "ext", "craigsfile~003.pdf"), array("here", "ext", "craigsfile~004.pdf"), array("here", "ext", "waynesfile~003.pdf") ); $newarray = sort_files($array); echo "<pre>"; var_dump($newarray); echo "</pre>"; ?> Orio. Quote Link to comment Share on other sites More sharing options...
Cep Posted November 8, 2007 Author Share Posted November 8, 2007 Hi Orio, Well I was trying to make the name of the container a variable value equal to the filename. The code does actually do this with an array but because filename is a string it throws the notices at you. However I did think the implementation was a bit sketchy at best. Thanks for your help Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.