up vote 6 down vote favorite

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

link|flag

2  
Do you just want the string length or would you want the strings as well? – NullUserException 2 days ago
I guess both are useful – Mark 2 days ago

5 Answers

up vote 3 down vote accepted

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

Live example

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

link|flag
+1 Neat. You could just do array_map('strlen', $data) though; – NullUserException 2 days ago
@NullUser - WOW! I don't know why, but I thought that didn't work >:( – Peter Ajtai 2 days ago
The only reason I didn't accept this on is becuase it uses 5.3.0, otherwise really neat! – Mark 2 days ago
@Mark - Well, now it doesn't... I don't know why, but for some bizarre reason, I thought you couldn't use native functions with an array map.... FACE PALM – Peter Ajtai 2 days ago
This is by far the cleanest solution, but it's not very efficient, as it traverses the array three times (array_map, min and max), so if you have huge arrays, better use a single loop. – Vinko Vrsalovic 2 days ago
show 1 more comment
up vote 3 down vote

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);
}
link|flag
This seems overly complex (with the addition of an initialized $min and $max), unless you are dealing with a huge array that you only want to traverse once. – Peter Ajtai 2 days ago
up vote 2 down vote
$min = 100;
$max = -1;

foreach($data as $a){
  $length = strlen($a);
  if($length > $max){ $max = $length; }
  else if($length < $min){ $min = $length; }
}
link|flag
it does exactly what the op wants. – Femaref 2 days ago
thank you fernaref – brian_d 2 days ago
that's right though. $min has to start at a very high number (int32.maxvalue perhaps?) – Femaref 2 days ago
true. have made edit – brian_d 2 days ago
Or you can set $max/$min to the first element and (optionally) skip it in the iteration. (Easier to do that with a for loop.) – konforce 2 days ago
show 2 more comments
up vote 1 down vote

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";
?>
link|flag
up vote 0 down vote
<?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);
link|flag

Your Answer

get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.