PHP 8.5.0 Alpha 1 available for testing

Voting

: eight minus four?
(Example: nine)

The Note You're Voting On

dan at aoindustries dot com
16 years ago
From an algorithmic efficiency standpoint, building an entire array of lengths to then sort to only retrieve the longest value is unnecessary work. The following should be O(n) instead of O(n log n). It could also be:

<?php
function get_longest_value($array) {
// Some don't like to initialize, I do
$longest = NULL;
$longestLen = -1;
foreach (
$array $value) {
$len = strlen($value);
if(
$len>$longestLen) {
$longest = $value;
$longestLen = $len;
}
}
$longest = str_replace("\r\n", "\n", $longest);
if (
get_magic_quotes_gpc()) { return stripslashes($longest); }
return
$longest;
}
?>

<< Back to user notes page

To Top