The Little Guy Posted April 23, 2009 Share Posted April 23, 2009 consisting of all ones and zeros (1110001101001) what is the best way to add up all of the 1's? Quote Link to comment Share on other sites More sharing options...
Bauer418 Posted April 23, 2009 Share Posted April 23, 2009 substr_count('1110001101001', '1'); Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 23, 2009 Share Posted April 23, 2009 Use str_split and array_sum <?php $str = '1110001101001'; echo array_sum(str_split($str)); ?> Ken Quote Link to comment Share on other sites More sharing options...
Bauer418 Posted April 23, 2009 Share Posted April 23, 2009 Seems like more work if substr_count works perfectly...just tested it. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted April 23, 2009 Author Share Posted April 23, 2009 Thanks! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.