Jump to content

Recommended Posts

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!

 

 

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";
    }
  }

?>

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 :P :P
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.