从索引数组中检索基于字母的数据

时间:2014-12-21 04:07:33

标签: php arrays

这是我的问题我有一个索引数组,其中包含n个数据,我想从特定字母开始获取数据。

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

例如:我需要从'R'开始获取数据

任何帮助都将非常感激。 谢谢!

1 个答案:

答案 0 :(得分:1)

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