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

发布于 2024-09-19 01:31:08 字数 246 浏览 8 评论 0原文

我有一个像这样的数组

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

如何根据字符串长度从中选择最小和最大值而不是数字值。 (本例中为 1(最小)和 3(最大)

I have an array like this

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

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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

怎樣才叫好 2024-09-26 01:31:09

看来你应该使用 array_map()< /strong>

  // 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 数组未排序,因此您可以轻松检索每个字符串长度对应的数字。

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);

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

一页 2024-09-26 01:31:09

以下是 brian_d 的代码的改进版本:

$min = PHP_INT_MAX;
$max = -1;

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

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

$min = PHP_INT_MAX;
$max = -1;

foreach ($data as $a) {
    $length = strlen($a);
    $max = max($max, $length);
    $min = min($min, $length);
}
扭转时空 2024-09-26 01:31:09

虽然在这种情况下不建议这样做,因为您将遍历数组两次,但您也可以使用 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;
}
?>

如果您使用 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";
?>

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

<?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
   $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";
?>
染墨丶若流云 2024-09-26 01:31:09
$min = 100;
$max = -1;

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

foreach($data as $a){
  $length = strlen($a);
  if($length > $max){ $max = $length; }
  else if($length < $min){ $min = $length; }
}
靖瑶 2024-09-26 01:31:09
<?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);
<?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);
那片花海 2024-09-26 01:31:09

为了完成这一点,这里有一个关于最大值和最小值的一行:

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

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

$maximum = max(array_map('strlen', $array));
$minimum = min(array_map('strlen', $array));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文