usort and strlen function problem
Posted: Mon Aug 22, 2011 12:03 pm
Hi i am trying to understand what this code is doing and struggling to understand how it works.
If someone
The parts im unsure of are listed below
-------- return ($1_a < $1_b) ? -1 : 1;
also not sure why its needs => in the array
lastly not sure why it needs a while loop
if someone could give me an clear explanation that would be great so i understand it.
cheers
If someone
The parts im unsure of are listed below
-------- return ($1_a < $1_b) ? -1 : 1;
also not sure why its needs => in the array
lastly not sure why it needs a while loop
if someone could give me an clear explanation that would be great so i understand it.
cheers
Code: Select all
<?php
function by_length($a, $b)
{
$1_a = strlen($a);
$1_b = strlen($b);
if ($1_a = = $1_b) {
return 0;
}
return ($1_a < $1_b) ? -1 : 1;
}
$countries = array("e" => "united states"
"d" => "united kindom"
"c" => "canada"
"b" => "costa rica"
"a" => "germany");
usort($countries, by_length);
while (list($key,$val)) = each ($countries))
{
echo "element $key equals $val <BR>\n";
}
?>