I have an array of hyperlinks being generated from ab object for display on a page. The link text is all that is displayed on the page. I need to sort the hyperlinks/link text alphabetically.
Here is what I have:
foreach ($value as $key1 => $value1) {
if ($key1 == 'id') {
$id = $value1;
}
if ($key1 == 'name') {
$link = '<a href="#" id="program_link_' . $id . '" class="program_link">' . $value1 . '</a><br>';
array_push($stack, $link);
}
}
asort($stack);
print_r($stack);
The asort call on $stack does not sort the array by link text.
I think this may call for a regexp on the subset of the hyperlink string in the array, and then a string compare and switch in the array, but am at a loss on how to do that in PHP.
Any ideas much appreciated.
According to the code given, the link text is what’s in
$value1. So you can sort based on that.Assuming that the link text can be used as an array key (doesn’t contain invalid key characters) you can add them to an array as such:
$links[$value1] = '<a href="#" id="program_link_' . $id . '" class="program_link">' . $value1 . '</a><br>';and then sort them by keyksort($links);