PHP 数组中最短/最长的字符串长度 [英] PHP shortest/longest string length 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).
推荐答案
看来你应该使用 array_map()
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屋!
查看全文