Hi,
I have a function like this:
function column_links() {
$column = scandir("content");
foreach ($column as $value) {
$stvalue = str_replace(".php", "", $value);
echo "<a href=\"{$stvalue}\">{$GLOBALS['tags'][$stvalue][title]}</a>";
}
}
and this is my variable:
define('title', 0);
define('desc', 1);
define('order', 2);
$tags = array(
'index' => array('My Page', 'this is a description', '1'),
'about' => array('About', 'this is a description', '2'),
'sitemap' => array('Site Map', 'this is a description', '3'));
I want to sort the produced links by "order" rather than by file name so my html would look like this:
<a href="index">My Page</a>
<a href="about">About</a>
<a href="sitemap">Site Map</a>
I thought that array_multisort would do the trick but I cant even figure out where to put it. Any ideas please?
Thank you.
You can use the array function usort to define your own sorts!
The way it works is that you write your own function to determine how the array should be sorted. The function should return an integer less than, equal to, or greater than zero to determine how the first argument compares to the second.
For example:
I write a custom function called order_sort
, which compares the ['order']
keys.
function order_sort($a, $b) {
if ($a['order'] > $b['order'])
return 1;
if ($a['order'] < $b['order'])
return -1;
return 0;
}
Then I can call usort on my array to sort it using my custom function.
usort($tags, 'order_sort');
Its keys will be discarded and the arrays will be sorted by ['order']
.
Your code is a little confusing, I hope I understood your example right. If not, I hope my example helped you write your own!