PHP数组中最短/最长的字符串 [英] PHP shortest/longest string in array
本文介绍了PHP数组中最短/最长的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个像这样的数组
$data = array(
"163",
"630",
"43",
"924",
"4",
"54"
);
如何根据字符串长度非数字值从中选择最小和最大值. (在此示例中为1(最小)和3(最大).
How can I select the smallest and largest values from it according to string length NOT number value. (for this example it is 1 (smallest) and 3 (largest).
推荐答案
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);
请注意,$lengths
数组未排序,因此您可以轻松地为每个字符串长度检索相应的数字.
Note that the $lengths
array is unsorted, so you can easily retrieve the corresponding number for each string length.
这篇关于PHP数组中最短/最长的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文