sabeti05 Posted July 28, 2015 Share Posted July 28, 2015 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: bca how can I change its output to: a b c thank you all! Link to comment https://forums.phpfreaks.com/topic/297503-how-to-sort-tags-inside-an-array-based-on-their-text/ Share on other sites More sharing options...
Barand Posted July 28, 2015 Share Posted July 28, 2015 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)); } Link to comment https://forums.phpfreaks.com/topic/297503-how-to-sort-tags-inside-an-array-based-on-their-text/#findComment-1517517 Share on other sites More sharing options...
QuickOldCar Posted July 28, 2015 Share Posted July 28, 2015 You should make a habit to separate coding logic from html. Only add html for display, not stored in arrays. Link to comment https://forums.phpfreaks.com/topic/297503-how-to-sort-tags-inside-an-array-based-on-their-text/#findComment-1517525 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.