2010-09-14 10 views
24

Mam tablicę jak tenPHP najkrótszy/najdłuższy ciąg w tablicy

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

Jak mogę wybrać ten najmniejszymi i największymi wartościami z niego według długości łańcucha NOT wartość liczbową. (w tym przykładzie jest to 1 (najmniejszy) i 3 (największy)

+2

Czy chcesz tylko długość struny, czy też chcesz struny? – NullUserException

+0

Sądzę, że oba są przydatne. –

+0

Powinieneś był usunąć zamieszanie długości/wartości, używając ciągów zamiast tych liczb. –

Odpowiedz

42

Wydaje się, należy użyć 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); 

Zauważ, że tablica $lengths jest nieposortowane, więc można łatwo pobrać odpowiedni numer dla każdej długości łańcucha.

+3

+1 Schludny. Możesz po prostu zrobić 'array_map ('strlen', $ data)' chociaż; – NullUserException

+0

@NullUser - WOW! Nie wiem dlaczego, ale myślałem, że to nie zadziałało> :( –

+0

Jedynym powodem, dla którego nie zaakceptowałem tego, jest to, że używa 5.3.0, w przeciwnym razie naprawdę schludnie! –

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

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

robi dokładnie to, czego chce operator. – Femaref

+0

dziękuję fernaref –

+0

to prawda. '$ min' musi rozpoczynać się od bardzo dużej liczby (int32.maxvalue maybe?) – Femaref

4

Oto ulepszona wersja 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

To wydaje się zbyt skomplikowane (z dodatkiem zainicjowanych '$ min' i' $ max'), chyba że masz do czynienia z ogromną tablicą, którą chcesz przejechać tylko raz . –

3

Chociaż w tym przypadku nie jest wskazane, bo ciebie” ll być przejeżdżające tablicę dwukrotnie, można również użyć array_reduce porównać każdy element przed resztą jak ten:.

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

Jeżeli jesteś PHP 5.3.0+ można wykorzystać 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);