Question

J'ai un tableau comme ceci

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

Comment puis-je sélectionner les plus petites et plus grandes valeurs il en fonction de la longueur de chaîne pas de valeur numérique. (pour cet exemple, il est de 1 (le plus petit) et 3 (le plus grand) .

Était-ce utile?

La solution

On dirait que vous devez utiliser un 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);

Notez que le tableau de $lengths est non triés, de sorte que vous pouvez facilement récupérer le numéro correspondant pour chaque longueur de la chaîne.

Autres conseils

Voici une version améliorée du code brian_d :

$min = PHP_INT_MAX;
$max = -1;

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

Bien que dans ce cas, il est conseillé de ne pas parce que vous allez traverser le tableau deux fois, vous pouvez également utiliser array_reduce pour comparer chaque élément contre le reste. Comme ceci:

<?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;
}
?>

Si vous utilisez PHP 5.3.0+, vous pouvez profiter de fermetures :

<?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";
?>
$min = 100;
$max = -1;

foreach($data as $a){
  $length = strlen($a);
  if($length > $max){ $max = $length; }
  else if($length < $min){ $min = $length; }
}
<?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);

Pour la fin, voici une seule ligne pour un maximum et minimum:

$maximum = max(array_map('strlen', $array));
$minimum = min(array_map('strlen', $array));
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top