... I need to display only the longest values of the array, or the shortest short order ...
That is not clear which word is considered long, and which is short, 'test' is long or short, krch will give examples, choose yourself:
For example, there is an array:
$ array = ["Ceylon", "Fish", "Apple", "MongoDB", "Seven77"];
1) Sort by number of characters:
usort($array, function ($a, $b) { return mb_strlen($b) - mb_strlen($a); }); var_dump($array);
Result:
array(5) { [0] => string(7) "MongoDB" [1] => string(7) "Seven77" [2] => string(6) "Ceylon" [3] => string(5) "Apple" [4] => string(4) "Fish" }
2) Only those values whose length is greater than or equal to $length = 5 :
$result = []; $length = 5; foreach ($array as $key => $value) { if ($length <= mb_strlen($value)) { $result[] = $value; } } var_dump($result);
Result:
array(4) { [0] => string(6) "Ceylon" [1] => string(5) "Apple" [2] => string(7) "MongoDB" [3] => string(7) "Seven77" }
3) Find the maximum length, and select the elements with this length:
$result = []; $length = 0; array_map(function($v) use (&$length) { if ($length < mb_strlen($v)) { $length = mb_strlen($v); } }, $array); foreach ($array as $key => $value) { if ($length == mb_strlen($value)) { $result[] = $value; } } var_dump($result);
Result:
array(2) { [0] => string(7) "MongoDB" [1] => string(7) "Seven77" }