/
, .
var_dump(explode('/', 'support/'));
: array(2) { [0]=> string(7) "support" [1]=> string(0) "" }
, 2! .
:
To fix this, we need to trim the string first. Instead of using count()
, explode
and trim
. We will use only substring_count()
and trim()
.
Here is the whole code:
function sort_url($a, $b)
{
if ($a == $b) {return 0;}
return substr_count(trim($a,'/'),'/') > substr_count(trim($b,'/'),'/') ? -1 : 1;
}
$array = ['support/maintenance', 'support/', 'support/faq/laminator'];
usort($array, "sort_url");
print_r($array);
Output
Array ( [0] => support/faq/laminator [1] => support/maintenance [2] => support/ )
source
share