【问题标题】:How to search an array for matching text如何在数组中搜索匹配的文本
【发布时间】:2015-08-19 19:47:42
【问题描述】:

想知道是否有搜索数组的函数,其中第一个字母与所选字母匹配。我可以做一些不那么优雅的事情,比如

Loop through array 
Each item remove the first character, match to chosen search variable   e.g if the first letter from apple, a equals my selection a, show. 

【问题讨论】:

标签: php arrays


【解决方案1】:

你的问题不清楚。但下面我给你一个例子,只选择包含你想要的第一个字母的数组中的选定元素:

function select_from_array($first_letter,$array){
    $return = Array();
    for($i=0;$i<count($array);$i++){
        if($array[$i][0] === $first_letter) $return[] = $array[$i];
    }
    return $return;
}

例子:

$arr = Array("Nice","Chops","Plot","Club");
print_r(select_from_array('C',$arr));

结果:

Array
(
    [0] => Chops
    [1] => Club
) 

【讨论】:

  • 您只需更改if 条件即可将此功能用于其他用途。
猜你喜欢
  • 2016-09-26
  • 2021-11-16
  • 2021-09-12
  • 2016-10-31
  • 2012-05-21
  • 1970-01-01
  • 2018-05-09
  • 2011-02-24
  • 1970-01-01
相关资源
最近更新 更多