Sujith Wayanad
Sujith Wayanad

Reputation: 543

retrieving alphabetic based data from an Indexed array

here is my question I have an Indexed array with n number of data I wanted to get data which starts from a particular letter.

array(
    1 => 'Jim'
    2 => 'Rao'
    3 => 'Mathew'
    4 => 'Raj'
    ...
)

eg: I need to get data starts from 'R'

any help will be really appreciated. Thanks!

Upvotes: 0

Views: 48

Answers (1)

Anas
Anas

Reputation: 5727

foreach ($array as $value)
{
    // we compare the first letter of every word in the array to R
    if (substr($value, 0, 1) == "R")
    {
        // if the first letter is R, we put the value in a new array $result
        $result[] = $value;
    }
}

Upvotes: 1

Related Questions