Jump to content

how to sort <a> tags inside an array based on their text?


sabeti05

Recommended Posts

Hi, I'm new here and in php,

I wrote this:

<?php
	$var = array('<a href="3">a</a>', '<a href="1">b</a>', '<a href="2">c</a>');
	sort($var);
	foreach($var as $value){
		echo $value, '<br>';
	}
?>

the output is:

b
c
a

 

how can I change its output to:

a

b

c

 

 

thank you all!

use a custom sort function with usort()

    $var = array('<a href="3">a</a>', '<a href="1">b</a>', '<a href="2">c</a>');
    usort($var, 'mysort');
    foreach($var as $value){
        echo $value, '<br>';
    }
    
    function mysort($a, $b)
    {
        return strcmp(strip_tags($a),strip_tags($b));
    }

Archived

This topic is now archived and is closed to further replies.

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