2010-09-14 15 views
24

Ho un serie come questoPHP più breve/lunga stringa nella matrice

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

Come posso selezionare i valori minimo e massimo da esso in base alla lunghezza della stringa NON valore numerico. (per questo esempio è 1 (il più piccolo) e 3 (il più grande)

+2

Vuoi solo la lunghezza della stringa o vuoi anche le stringhe? – NullUserException

+0

Suppongo che entrambi siano utili –

+0

Dovresti aver rimosso la confusione lunghezza/valore usando le stringhe al posto di questi numeri. –

risposta

42

Sembra che si dovrebbe usare 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); 

Si noti che la matrice è $lengths indifferenziati, in modo da poter facilmente recuperare il numero corrispondente per ogni lunghezza di stringa.

+3

+1 Neat. Potresti semplicemente fare 'array_map ('strlen', $ data)'; – NullUserException

+0

@NullUser - WOW! Non so perché, ma ho pensato che non funzionasse> :( –

+0

Il * solo * motivo per cui non ho accettato questo è perché utilizza 5.3.0, altrimenti davvero pulito! –

2
$min = 100; 
$max = -1; 

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

fa esattamente ciò che l'op vuole. – Femaref

+0

grazie fernaref –

+0

questo è giusto però. '$ min' deve iniziare con un numero molto alto (int32.maxvalue forse?) – Femaref

4

Ecco una versione migliorata del 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); 
} 
+0

Questo sembra eccessivamente complesso (con l'aggiunta di un inizializzato '$ min' e' $ max'), a meno che non si abbia a che fare con un enorme array che si desidera attraversare solo una volta . –

3

Anche se in questo caso non è consigliabile perché si' ll essere attraversando la matrice due volte, è anche possibile utilizzare array_reduce per confrontare ogni elemento contro il resto ti piace questa:.

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

Se sei utilizzando PHP 5.3.0+ è possibile usufruire di 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"; 
?> 
1
<?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); 
Problemi correlati