3v4l.org

run code in 500+ PHP versions simultaneously
<?php $array = array( 'Google', 'Facebook', "Test1112", 'Twitter', 'Slack', 'Twilio' ); usort($array, function ($a, $b) { return strlen($a) < strlen($b); }); $item = reset($array); $result = []; if ($item) { $len = strlen($item); foreach($array as $value) { if (strlen($value) === $len) { $result[] = $value; continue; } break; } } print_r($result);
Output for 8.2.0 - 8.2.30, 8.3.0 - 8.3.30, 8.4.1 - 8.4.18, 8.5.0 - 8.5.3
Deprecated: usort(): Returning bool from comparison function is deprecated, return an integer less than, equal to, or greater than zero in /in/q5d8X on line 12 Array ( [0] => Facebook [1] => Test1112 )
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.34
Deprecated: usort(): Returning bool from comparison function is deprecated, return an integer less than, equal to, or greater than zero in /in/q5d8X on line 14 Array ( [0] => Facebook [1] => Test1112 )
Output for 7.1.0 - 7.1.33, 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33
Array ( [0] => Facebook [1] => Test1112 )

preferences:
93.89 ms | 1709 KiB | 4 Q