我想从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

查看:74
本文介绍了我想从PHP 5.3.0中的数组中读取最长的字符串.我已经尝试过max函数,但似乎没有按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是代码的一部分.

$DatabaseArray=Array("Cooking","look","I cant","Walking the dog to the park","washing","talk","one");

$largest = max($DatabaseArray);

echo $largest. "<br />";

我想输出数组中最长的句子.

I would like to output the longest sentence in the array.

推荐答案

$lengths = array_map('strlen', $DatabaseArray);
$maxLength = max($lengths);
$index = array_search($maxLength, $lengths);
echo $DatabaseArray[$index];

这篇关于我想从PHP 5.3.0中的数组中读取最长的字符串.我已经尝试过max函数,但似乎没有按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆