t_k_eoh Posted August 2, 2007 Share Posted August 2, 2007 hi, I've browsed through the entire forum and I still can't an answer to this problem. I have a few words in an array and I would like to find all the words that begins with letter "a"? eg. (in the array) aim are am boy car cat ag. output is: aim are am There are some samples using substr but I am not sure how to use it. Can someone pls advice? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/62972-how-to-list-out-all-words-beginning-with-a-in-array/ Share on other sites More sharing options...
teng84 Posted August 2, 2007 Share Posted August 2, 2007 have you tried using array sort??? Quote Link to comment https://forums.phpfreaks.com/topic/62972-how-to-list-out-all-words-beginning-with-a-in-array/#findComment-313596 Share on other sites More sharing options...
teng84 Posted August 2, 2007 Share Posted August 2, 2007 ok try $fruits = array("lemon","orange","banana","apple"); asort($fruits); foreach ($fruits as $key => $val) { if (substr($val,0,1==a)){ echo "$key = $val\n"; } } Quote Link to comment https://forums.phpfreaks.com/topic/62972-how-to-list-out-all-words-beginning-with-a-in-array/#findComment-313597 Share on other sites More sharing options...
trq Posted August 2, 2007 Share Posted August 2, 2007 No need to sort if you only want the values starting with a particular letter... <?php $fruits = array("lemon","orange","banana","apple"); foreach ($fruits as $val) { if (substr($val,0,1) == 'a') { echo "$val\n"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/62972-how-to-list-out-all-words-beginning-with-a-in-array/#findComment-313609 Share on other sites More sharing options...
teng84 Posted August 2, 2007 Share Posted August 2, 2007 No need to sort if you only want the values starting with a particular letter... <?php $fruits = array("lemon","orange","banana","apple"); foreach ($fruits as $val) { if (substr($val,0,1) == 'a') { echo "$val\n"; } } ?> i sort it to make the loop shorter i forgot to break the loop once it reach the word not ok try $fruits = array("lemon","orange","banana","apple"); asort($fruits); foreach ($fruits as $key => $val) { if (substr($val,0,1==a)){ echo "$key = $val\n"; } else { break; } } starting with a :P :P Quote Link to comment https://forums.phpfreaks.com/topic/62972-how-to-list-out-all-words-beginning-with-a-in-array/#findComment-313613 Share on other sites More sharing options...
trq Posted August 2, 2007 Share Posted August 2, 2007 Theres probably more overhead in the actual sort call though it may be a valid point. Quote Link to comment https://forums.phpfreaks.com/topic/62972-how-to-list-out-all-words-beginning-with-a-in-array/#findComment-313614 Share on other sites More sharing options...
teng84 Posted August 2, 2007 Share Posted August 2, 2007 looping really makes the loading slow specially when it comes with bigger number so i always look if i can minimize or even remove the loop Quote Link to comment https://forums.phpfreaks.com/topic/62972-how-to-list-out-all-words-beginning-with-a-in-array/#findComment-313615 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.