Read the longest string from an array in PHP 5.3
$lengths = array_map('strlen', $DatabaseArray);
$maxLength = max($lengths);
$index = array_search($maxLength, $lengths);
echo $DatabaseArray[$index];
PHP Get Longest String in Array without loop
You could sort the strings by length using for example usort and get the first item using reset.
$array = array(
'Google',
'Facebook',
'Twitter',
'Slack',
'Twilio',
);
usort($array, function ($a, $b) {
return strlen($a) < strlen($b);
});
echo reset($array); // Facebook
If there could be more strings with equal length, you could use a foreach and break out of the loop when the length is not equal to the current length of the item to prevent looping the whole list.
$item = reset($array);
$result = [];
if ($item) {
$len = strlen($item);
foreach($array as $value) {
if (strlen($value) === $len) {
$result[] = $value;
continue;
}
break;
}
}
print_r($result);
Result
Array
(
[0] => Facebook
[1] => Test1112
)
Php demo
PHP shortest/longest string length in array
Seems like you should use an array_map()
// Convert array to an array of string lengths
$lengths = array_map('strlen', $data);
// Show min and max string length
echo "The shortest is " . min($lengths) .
". The longest is " . max($lengths);
Note that the $lengths
array is unsorted, so you can easily retrieve the corresponding number for each string length.
I just started learning PHP. How to find and print the longest word in a PHP array
$array =array("AB","ABC","ABCD","ABCDE","BD");
$longstring = $array[0];
foreach( $array as $string ) {
if ( strlen( $string ) > strlen( $longstring ) ) {
$longstring = $string;
}
}
echo $longstring;
I just started learning PHP. How to find and print the longest word in a PHP array
$array =array("AB","ABC","ABCD","ABCDE","BD");
$longstring = $array[0];
foreach( $array as $string ) {
if ( strlen( $string ) > strlen( $longstring ) ) {
$longstring = $string;
}
}
echo $longstring;
Php associative array sort and get key with highest length
You can use array_keys
and pass a second parameter to filter the returned keys to only include the max
ones. You can then find the longest key by using array_reduce
and a function that checks the lengths of the strings and throws out the shortest one, like so:
$array = array(
'abc' => 10,
'def' => 8,
'fff' => 3,
'abcr' => 10,
'adsfefs' => 10
);
$keys = array_keys($array, max($array));
$longestKey = array_reduce($keys, function ($a, $b) { return strlen($a) > strlen($b) ? $a : $b; });
var_dump($longestKey);
Be aware that if there are two or more strings that are the same length, it will return the last one.
Longest word in a string
Loop through the words of the string, keeping track of the longest word so far:
<?php
$string = "Where did the big Elephant go?";
$words = explode(' ', $string);
$longestWordLength = 0;
$longestWord = '';
foreach ($words as $word) {
if (strlen($word) > $longestWordLength) {
$longestWordLength = strlen($word);
$longestWord = $word;
}
}
echo $longestWord;
// Outputs: "Elephant"
?>
Can be made a little more efficient, but you get the idea.
PHP: Sort an array by the length of its values?
Use http://us2.php.net/manual/en/function.usort.php
with this custom function
function sort($a,$b){
return strlen($b)-strlen($a);
}
usort($array,'sort');
Use uasort if you want to keep the old indexes, use usort if you don't care.
Also, I believe that my version is better because usort is an unstable sort.
$array = array("bbbbb", "dog", "cat", "aaa", "aaaa");
// mine
[0] => bbbbb
[1] => aaaa
[2] => aaa
[3] => cat
[4] => dog
// others
[0] => bbbbb
[1] => aaaa
[2] => dog
[3] => aaa
[4] => cat
Is there a way to find out how deep a PHP array is?
This should do it:
<?php
function array_depth(array $array) {
$max_depth = 1;
foreach ($array as $value) {
if (is_array($value)) {
$depth = array_depth($value) + 1;
if ($depth > $max_depth) {
$max_depth = $depth;
}
}
}
return $max_depth;
}
?>
Edit: Tested it very quickly and it appears to work.
Related Topics
Move the Variation Price Location in Woocommerce
How to Get a List of Databases
Does PHP Support Conjunction and Disjunction Natively
Limiting Number of Times a Loop Runs in PHP
PHP Array, Are Array Indexes Case Sensitive
Cakephp Cake_Core_ Cache Was Unable to Write 'Cake_Dev_En-Us'
$_Post Empty on Utf-8 Characters
Regex PHP: Find Everything in Div
How to Tell Apart Simplexml Objects Representing Element and Attribute
Set Httponly and Secure on PHPsessid Cookie in PHP
Extending Session Timeout in PHP via the .Htaccess
Maximum Length of Generated Hash When Using Password_Hash
Change Pay Button on Checkout Based on Woocommerce Chosen Payment Method
PHP Loop Counter Bootstrap Row