PHP 5.3 从数组中读取最长的字符串 [英] Read the longest string from an array in PHP 5.3

查看:28
本文介绍了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屋!

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