Jump to content

[SOLVED] array to string conversion notices


Cep

Recommended Posts

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>";
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.