Re: array.sorting.php
| From: | John Taylor-Johnston | Date: | Tue, 04 Sep 2012 19:33:00 +0000 |
| Subject: | Re: array.sorting.php | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-318993@lists.php.net to get a copy of this message | ||
Matijn Woudt wrote:
Taken from the natsort manual page comments: <?php /** * keyNatSort does a natural sort via key on the supplied array. *I've tried it. I don't see it sorting anything? http://cegepsherbrooke.qc.ca/~languesmodernes/test/keynatsort.php $words = preg_split('/[[:space:]]+/',$mynewstring); foreach ($words as $word) { $freq[$word]++; } function keyNatSort($array, $saveMemory=false) { if(is_array($array)) {* @param $array The array to natural sort via key.* @param $saveMemory If true will delete values from the original array as it builds the sorted array. * @return Sorted array on success. Boolean false if sort failed or null if the object was not an array. */ PS. Please bottom post on this and probably any mailing list
$keys = array_keys($array);
if(natsort($keys))
{
$result = array();
foreach($keys as $key)
{
$result[$key] = $array[$key];
if($saveMemory)
unset($array[$key]);
}
}
else
$result = false;
}
else
$result = null;return $result; } keyNatSort($freq); echo "<table>\n"; foreach ($freq as $key => $val) { # echo "$key = $val <br />\n"; echo "<tr><td>".str_replace("_", " ", $key)."</td><td>$val</td></tr>\n"; } echo "</table>\n";