按首字母过滤数组

编程入门 行业动态 更新时间:2024-10-27 19:22:39
本文介绍了按首字母过滤数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在建立一个平台.在我的代码中的某个地方,有一个看起来像这样的数组(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

更多推荐

按首字母过滤数组

本文发布于:2023-10-23 19:35:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1521813.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   首字母

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!