如何在php中搜索数组? [英] how to search an array in php?

查看:35
本文介绍了如何在php中搜索数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个名称数组,我想要的是我想针对字符串或正则表达式搜索此特定数组,然后将找到的匹配项存储在另一个数组中。这可能吗 ?如果是,那么您能给我提示吗?我是编程新手。

suppose I have an array of names, what I want is that I want to search this particular array against the string or regular expression and then store the found matches in another array. Is this possible ? if yes then please can your give me hint ? I am new to programming.

推荐答案

您需要di的方法是使用如下回调函数映射数组:

what you would need to di is map the array with a callback like so:

array_filter($myarray,"CheckMatches");

function CheckMatches($key,$val)
{
    if(preg_match("...",$val,$match))
    {
        return $match[2];
    }
}

这将为数组中的每个元素运行回调!

This will run the callback for every element in the array!

已更新为 array_filter

这篇关于如何在php中搜索数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆