PHP 数组中最短/最长的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3713517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:48:57  来源:igfitidea点击:

PHP shortest/longest string in array

phparraysstringstring-length

提问by Mark Lalor

I have an arraylike this

我有一个这样的数组

$data = array(
    "163",
    "630",
    "43",
    "924",
    "4",
    "54"
);

How can I select the smallest and largest valuesfrom it according to string lengthNOT number value. (for this example it is 1 (smallest) and 3 (largest).

我怎样才能选择最小值和最大值从它根据字符串长度不数值。 (对于这个例子,它是 1(最小)和 3(最大)

回答by Peter Ajtai

Seems like you should use an array_map()

似乎您应该使用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);

Note that the $lengthsarray is unsorted, so you can easily retrieve the corresponding number for each string length.

请注意,该$lengths数组是未排序的,因此您可以轻松检索每个字符串长度的相应数字。

回答by NullUserException

Here's an improved version of brian_d's code:

这是brian_d 代码的改进版本:

$min = PHP_INT_MAX;
$max = -1;

foreach ($data as $a) {
    $length = strlen($a);
    $max = max($max, $length);
    $min = min($min, $length);
}

回答by Vinko Vrsalovic

Although in this case it is not advisable because you'll be traversing the array twice, you can also use array_reduceto compare each element against the rest. Like this:

尽管在这种情况下不建议这样做,因为您将遍历数组两次,但您也可以使用array_reduce将每个元素与其余元素进行比较。像这样:

<?php

$data = array('163','630','43','42','999','31');
//Will return the longest element that is nearest to the end of the array (999)
//That's why we use strlen() on the result.
$max_l = strlen(array_reduce($data,'maxlen'));
//Will return the shortest element that is nearest to the end of the array (31)
$min_l = strlen(array_reduce($data,'minlen'));

echo "The longest word is $max_l characters, while the shortest is $min_l\n";

function maxlen($k,$v) {
        if (strlen($k) > strlen($v)) return $k;
        return $v;
}
function minlen($k,$v) {
        if ($k == '') return PHP_INT_MAX;
        if (strlen($k) < strlen($v)) return $k;
        return $v;
}
?>

If you are using PHP 5.3.0+ you can take advantage of closures:

如果您使用的是 PHP 5.3.0+,您可以利用闭包

<?php
   $max_l = strlen(array_reduce($data,
                function ($k,$v) { return (strlen($k) > strlen($v)) ? $k : $v; }
        ));

   $min_l = strlen(array_reduce($data,
                function ($k,$v) {
                        if (!$k) return PHP_INT_MAX;
                        return (strlen($k) < strlen($v)) ? $k : $v;
                }
        ));

echo "The longest word is $max_l characters, while the shortest is $min_l\n";
?>

回答by brian_d

$min = 100;
$max = -1;

foreach($data as $a){
  $length = strlen($a);
  if($length > $max){ $max = $length; }
  else if($length < $min){ $min = $length; }
}

回答by Wrikken

<?php
$array = array(
    "163",
    "630",
    "43",
    "924",
    "4",
    "54"
);
$arraycopy  = array_map('strlen',$array);
asort($arraycopy);

$min = reset($arraycopy);

//if you need a single 'minword'
$minword = $array[key($arraycopy)];
//if you need them all
$minwords = array_intersect_key($array,array_flip(array_keys($arraycopy,$min)));


$max = end($arraycopy);
//if you need a single 'maxword'
$maxword = $array[key($arraycopy)];
//if you need them all:
$maxwords = array_intersect_key($array,array_flip(array_keys($arraycopy,$max)));

var_dump($min,$max,$minword,$maxword,$minwords,$maxwords);

回答by Typewar

For completion, here is a one-liner for maximum and minimum:

为了完成,这里是最大和最小的单行:

$maximum = max(array_map('strlen', $array));
$minimum = min(array_map('strlen', $array));