Array error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pedroz
Forum Commoner
Posts: 99
Joined: Thu Nov 03, 2005 6:21 am

Array error

Post 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...
Last edited by pedroz on Sun Mar 05, 2006 1:11 pm, edited 1 time in total.
User avatar
ambivalent
Forum Contributor
Posts: 173
Joined: Thu Apr 14, 2005 8:58 pm
Location: Toronto, ON

Post by ambivalent »

Put this right before your last echo:

Code: Select all

echo gettype($array_strlen);
pedroz
Forum Commoner
Posts: 99
Joined: Thu Nov 03, 2005 6:21 am

Post by pedroz »

Nop... It displays 1.

It might be good to rest for a while after several hours without sleepin :(
User avatar
ambivalent
Forum Contributor
Posts: 173
Joined: Thu Apr 14, 2005 8:58 pm
Location: Toronto, ON

Post 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.
pedroz
Forum Commoner
Posts: 99
Joined: Thu Nov 03, 2005 6:21 am

Post by pedroz »

Right...

rsort($array_strlen);

instead

$array_strlen = rsort($array_strlen);

because rsort is boolean function :roll:
User avatar
ambivalent
Forum Contributor
Posts: 173
Joined: Thu Apr 14, 2005 8:58 pm
Location: Toronto, ON

Post 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>";
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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."
Post Reply