Page 1 of 1

Array error

Posted: Sun Mar 05, 2006 12:47 pm
by pedroz
I want to display the max strlen word in the following string:
$array_strlen = "02 10dd 6abcd 8ss";

Code: Select all

$array_strlen = "02 10ff 6fffd 8ss";

$array_strlen = array_unique(explode(' ',$array_strlen));
 
$array_strlen = array_map('strlen', $array_strlen);

$array_strlen = rsort($array_strlen);

echo $array_strlen[0];
What's wrong ? It "should" display 5...

Posted: Sun Mar 05, 2006 1:11 pm
by ambivalent
Put this right before your last echo:

Code: Select all

echo gettype($array_strlen);

Posted: Sun Mar 05, 2006 1:18 pm
by pedroz
Nop... It displays 1.

It might be good to rest for a while after several hours without sleepin :(

Posted: Sun Mar 05, 2006 1:25 pm
by ambivalent
pedroz wrote:Nop... It displays 1.
Then something is not right.
The manual wrote:Possible values for the returned string are:

"boolean" (since PHP 4)
"integer"
"double" (for historical reasons "double" is returned in case of a float, and not simply "float")
"string"
"array"
"object"
"resource" (since PHP 4)
"NULL" (since PHP 4)
"user function" (PHP 3 only, deprecated)
"unknown type"
I get "boolean" when I run the code you posted.

Posted: Sun Mar 05, 2006 1:28 pm
by pedroz
Right...

rsort($array_strlen);

instead

$array_strlen = rsort($array_strlen);

because rsort is boolean function :roll:

Posted: Sun Mar 05, 2006 1:46 pm
by ambivalent
Therefore, you don't have an array yet you are trying to output an array element?

Code: Select all

echo $array_strlen[0];

Code: Select all

echo "<pre>";
print_r($array_strlen);  //prints "1"
echo "</pre>";

Posted: Sun Mar 05, 2006 1:48 pm
by John Cartwright

Code: Select all

$array_strlen = "02 10ff 6fffd 8ss";

$array_strlen = array_unique(explode(' ',$array_strlen));
 
$array_strlen = array_map('strlen', $array_strlen);

rsort($array_strlen);

echo $array_strlen[0];
Check the rsort function, it "Returns TRUE on success or FALSE on failure."