I’m trying to use ‘array_combine’ to do what it says on the tin, but it’s not working for me.
I have a keys array ($order), which prints (using ‘print_r’) as:
Array ( [0] => 24889 [1] => 24929 [2] => 24961 [3] => 24834 [4] => 24802 [5] => 24914 [6] => 24865 [7] => 24897 [8] => 24953 [9] => 24946 [10] => 24937 )
(the numbers are drived from the file’s last modification date using ‘filemtime’),
and a values array ($files), which prints as:
Array ( [0] => HMI Apr 09 Newsletter.html [1] => HMI Aug 09 Newsletter.html [2] => HMI Dec 09 Newsletter.html [3] => HMI Feb 09 Newsletter.html [4] => HMI Jan 09 Newsletter.html [5] => HMI Jun 09 Newsletter.html [6] => HMI Mar 09 Newsletter.html [7] => HMI May 09 Newsletter.html [8] => HMI Nov 09 Newsletter.html [9] => HMI Oct 09 Newsletter.html [10] => HMI Sep 09 Newsletter.html )
Then I have:
$newdir = array_combine($order, $files);
print_r($newdir);
but it prints as:
Array ( [0] => HMI Apr 09 Newsletter.html [1] => HMI Aug 09 Newsletter.html [2] => HMI Dec 09 Newsletter.html [3] => HMI Feb 09 Newsletter.html [4] => HMI Jan 09 Newsletter.html [5] => HMI Jun 09 Newsletter.html [6] => HMI Mar 09 Newsletter.html [7] => HMI May 09 Newsletter.html [8] => HMI Nov 09 Newsletter.html [9] => HMI Oct 09 Newsletter.html [10] => HMI Sep 09 Newsletter.html )
which is the same as ‘$files’ above, and not what I need.
I’m expecting (or hoping for) a result that would print as:
Array ( [24889]=> HMI Apr 09 Newsletter.html [24929]=> HMI Aug 09 Newsletter.html [24961]=> HMI Dec 09 Newsletter.html [24834]=> HMI Feb 09 Newsletter.html [24802]=> HMI an 09 Newsletter.html [24914]=> HMI Jun 09 Newsletter.html [24865]=> HMI Mar 09 ewsletter.html [24897]=> HMI May 09 Newsletter.html [24953]=> HMI Nov 09 Newsletter.html [24946]=> HMI Oct 09 Newsletter.html [24937]=> HMI Sep 09 Newsletter.html )
which I would then sort to put the Newsletters in the correct order (by month).
Can someone tell me what I’ve got wrong, please ?
Why not just do:
$array = array();
...
{
$key = substr(filemtime('archive/newsletters/2009/' . $file),-5);
$array[$key] = $file;
}
Thanks for an incredibly quick response !
I’m unsure what snippet I could send you that wasn’t in my first posting. The printed out versions of the arrays are derived from:
print_r($order);
print_r($files);
so you’d expect
$newdir = array_combine($order, $files);
print_r($neworder);
to give the same result as you got, wouldn’t you ?
My script is intended to list the archived files from a directory on a web site, where the file names allocated don’t automatically put the files in chronological order (instead you get Apr, Aug, Feb etc in alpha order of months). So I’ve read the file names into one array, and a truncated form of ‘filemtime’ into another. Now I’m trying to combine them in one array, and then sort that array by the new index to get the chronological order (I have to assume the mod dates are in sequence).
Here’s the whole thing to date. Note it’s incomplete !
<?php
echo "This is nl.php";
?>
<h1>Newsletter Index</h1>
<h2>2009</h2>
<ul>
<?php
if ($handle = opendir('archive/newsletters/2009')) {;
while (false !== ($file = readdir($handle))) {
if (preg_match('/\\.html$/',$file)) {
$files[] = $file;
// echo "filename is " . $file . "<br />";
$order[] = substr(filemtime('archive/newsletters/2009/' . $file),-5);
}
}
$newdir = array_combine($order, $files);
$sdir = sort($newdir);
print_r($order); echo "<br /><br />";
print_r($files); echo "<br /><br />";
print_r($newdir); echo "<br /><br />";
//print_r($sdir); echo "<br /><br />";
// echo "true";
// foreach ($sdir as $value) {
// if (preg_match('/\\.html$/',$value)) {
// echo "<li>" . $value . "</li>\
";
// }
// }
} else {
echo "not found";
}
?>
</ul>
<h2>2010</h2>
Thanks everyone.
I don’t know why it should work for you and not for me ! Often in such circumstances it’s a typo, but I’ve not found one yet. It may be because my ‘keys’ array ($order) already has its own index, and perhaps I should force ‘values’ as Anthony suggests.
However, Jaanboy’s suggestion cuts out several lines of code, avoiding ‘array_combine’ altogether, and I’m going to try that first.
Can you give us a quick snippet which demonstrates this (mis)behaviour? Given what you’ve described, it works fine for me.
<?php
$keys = array(24889,24929,24961,24834,24802,24914,24865,24897,24953,24946,24937);
$vals = array(
'HMI Apr 09 Newsletter.html',
'HMI Aug 09 Newsletter.html',
'HMI Dec 09 Newsletter.html',
'HMI Feb 09 Newsletter.html',
'HMI Jan 09 Newsletter.html',
'HMI Jun 09 Newsletter.html',
'HMI Mar 09 Newsletter.html',
'HMI May 09 Newsletter.html',
'HMI Nov 09 Newsletter.html',
'HMI Oct 09 Newsletter.html',
'HMI Sep 09 Newsletter.html'
);
print_r(array_combine($keys, $vals));
?>
Gives
Array
(
[24889] => HMI Apr 09 Newsletter.html
[24929] => HMI Aug 09 Newsletter.html
[24961] => HMI Dec 09 Newsletter.html
[24834] => HMI Feb 09 Newsletter.html
[24802] => HMI Jan 09 Newsletter.html
[24914] => HMI Jun 09 Newsletter.html
[24865] => HMI Mar 09 Newsletter.html
[24897] => HMI May 09 Newsletter.html
[24953] => HMI Nov 09 Newsletter.html
[24946] => HMI Oct 09 Newsletter.html
[24937] => HMI Sep 09 Newsletter.html
)
P.S. I’d be interested to know more about what your script is doing as there may be easier/better/neater ways to arrive at an array containing a mapping of file modification times to filenames.
Strange, but it appears to be fine here.
<?php
$one = array(
'chalk',
'cheese'
);
$two = array(
'foo',
'bar'
);
print_r(
array_combine(
$one,
$two
)
);
/*
Array
(
[chalk] => foo
[cheese] => bar
)
*/
?>
What happens if your force array_values?
<?php
$one = array(
'chalk',
'cheese'
);
$two = array(
'foo',
'bar'
);
print_r(
array_combine(
array_values($one),
array_values($two)
)
);
/*
Array
(
[chalk] => foo
[cheese] => bar
)
*/
?>