从PHP 5.3中的数组中读取最长的字符串 [英] Read the longest string from an array in PHP 5.3
本文介绍了从PHP 5.3中的数组中读取最长的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是代码的一部分.
$DatabaseArray=Array("Cooking","look","I cant","Walking the dog to the park","washing","talk","one");
$largest = max($DatabaseArray);
echo $largest. "<br />";
我想从PHP 5.3.0中的数组中读取最长的字符串.我已经尝试过max函数,但似乎没有按预期工作.
I would like to read the longest string from an array in PHP 5.3.0. I've tried the max function but doesnt seem to work as expected.
推荐答案
$lengths = array_map('strlen', $DatabaseArray);
$maxLength = max($lengths);
$index = array_search($maxLength, $lengths);
echo $DatabaseArray[$index];
这篇关于从PHP 5.3中的数组中读取最长的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文