Guardian-Mage Posted February 16, 2008 Share Posted February 16, 2008 I have an array with a few values. This array is called $array. I want to find the biggest string in the array, and make all of the other strings in the array the same length by adding whitespace. How should I go about doing this, and I know how to add whitespace, just not how to find the biggest string and do everything else. For example: $array[0] = "asmllstr"; #8 Characters $array[1] = "amediumstri"; #11 Characters $array[2] = "abiggerstring"; #13 Characters $array[3] = "thisisclearlybiggest"; #20 Characters $array[4] = "smaller"; #7 Characters #This would become $array[0] = "asmllstr "; #20 Characters $array[1] = "amediumstri "; #20 Characters $array[2] = "abiggerstring "; #20 Characters $array[3] = "thisisclearlybiggest"; #20 Characters $array[4] = "smaller "; #20 Characters Thanks Link to comment https://forums.phpfreaks.com/topic/91359-even-length-strings/ Share on other sites More sharing options...
sasa Posted February 16, 2008 Share Posted February 16, 2008 try <?php $array[0] = "asmllstr"; #8 Characters $array[1] = "amediumstri"; #11 Characters $array[2] = "abiggerstring"; #13 Characters $array[3] = "thisisclearlybiggest"; #20 Characters $array[4] = "smaller"; #7 Characters $max_len = -1; foreach ($array as $s){ $x = strlen($s); if ($x > $max_len){ $max_len = $x; } } foreach ($array as $k => $v) $array[$k] = str_pad($v, $max_len); ?> Link to comment https://forums.phpfreaks.com/topic/91359-even-length-strings/#findComment-468139 Share on other sites More sharing options...
Guardian-Mage Posted February 16, 2008 Author Share Posted February 16, 2008 thanks Link to comment https://forums.phpfreaks.com/topic/91359-even-length-strings/#findComment-468141 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.