我正在建立一个平台.在我的代码中的某个地方,有一个看起来像这样的数组(PHP):
I'm building a platform. Somewhere in my code, there's an array that looks like this (PHP):
$entries = array('p01','p02','g01','g02','a001','a002')我需要编写一个脚本,该脚本根据第一个字母过滤数组.例如,问那些以"p"开头的人会给我
I need to write a script that filters the array based on the first letter. For example, asking for those with the starting letter "p" would give me
$filtered_entries = array('p01','p02');类似地,如果我要问那些以"g"或"a"开头的字母,那也会给我.知道如何实现吗?
Similarly, if I asked for those with starting letter "g" or "a" it would give me those as well. Any idea how to accomplish this?
推荐答案PHP中有一个array_filter()函数,可用于完成此任务:
There is an array_filter() function in PHP which you can use to accomplish this:
$filtered = array_filter($array, create_function('$a', 'return $a[0] == "' . $letter . '";'));
我将留给您概括处理所有字母的功能.
请参阅: www.php/manual/zh/function.array-filter.php
更多推荐
按首字母过滤数组
发布评论