需要补充的字符串部分匹配查找在foreach循环查找数组键 [英] Need to add string partial match look up in foreach loop to find array keys

查看:145
本文介绍了需要补充的字符串部分匹配查找在foreach循环查找数组键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个有点PHP code的这条 - 从输入字符串的冲刺。该数组的键可能会或可能不会有一个 - 每个字之间。因此,如果字符串有空格或破折号,字符串依然匹配的关键。

现在我需要扩大这一点有一个部分匹配定位阵列中的键

下面是我有这么远。<​​/ P>

我的阵列

  $页面=阵列(管理员= GT;阵列(
    网络管理员=&GT;阵列('标题'=&GT;'网络'$ li_1,'描述'=方式&gt;网络$ li_1''$ temp_content,'后'=&GT;'。&LT; P&GT; $网络li_1 ''。$ temp_content。'&LT; / p&GT;'),
    数据库管理员'=&GT;阵列('标题'=&GT;'数据库'$ li_1,'描述'=&GT;。'数据库'$ li_1''$ temp_content,'后'=&GT;'&LT; P&GT;数据库$ li_1 ''。$ temp_content。'&LT; / p&GT;'),
)'分析师'=&GT;阵列(
    业务系统分析师'=&GT;阵列('标题'=方式​​&gt;业务系统$ li_2,'描述'=&GT;业务系统$ li_2''$ temp_content,'后'=&GT;'&LT; P&GT;业务系统 。$ li_2'&LT'$ temp_content。'; / p&GT;'),
    数据分析= GT;阵列('标题'=&GT;'数据'$ li_2,'描述'=&GT;'数据'$ li_2'后'=&GT;'。'$ temp_content。'&LT; P&GT;数据'$ li_2 ''。$ temp_content。'&LT; / p&GT;'),
));

PHP foreach循环

  $ t_keys =阵列();的foreach($页为$ K =&GT; $ ARR2){
    的foreach(array_keys($ ARR2)为$ a){
        $ new_key = str_replace函数( - ,,$一个);
        $ t_keys [$ new_key] =阵列($ K,$ A);
    }
}
$海峡= str_replace函数(阵列(' - ','和'),'',用strtolower($位置));
如果(array_key_exists($海峡,$ t_keys)){
    $目标= $页[$ t_keys [$海峡] [0] [$ t_keys [$海峡] [1]];
}

$ t_keys [$ STR] [0] 访问键管理​​ $ t_keys [$ STR] [1] 访问子键网​​络管理员

我需要做的是从URL查询字符串匹配传入的字符串来查找数组的子密钥匹配。

例部分匹配的字符串,来了的是 Oracle数据库管理员。这关键是不是在我的数组,但 Oracle管理员是,以及数据库管理员

所以,我怎么能匹配字符串进来的子键在数组中是否存在类似的话?

下面是进入的部分匹配串在我的错误日志来的小单子。

 
苹果
CSS
.NET开发人员
桌面
。净
蟒蛇
苹果
数据中心
手机技师
SQL数据库开发人员
jQuery的
德尔福
CSS
蟒蛇
个人计算机
软件
XML
网站管理员
研究和发展
Python编程
计算机技术人员或技术人员计算机
。净
C

在某些情况下,上面的部分匹配不随地阵列中存在。我只是做那些重定向。但是,其他如数据中心 Python编程只是作为一个例子是阵列中的

示例字符串数据中心,阵列中最匹配的是数据中心技术人员和最近的匹配字符串 Python编程 Python开发

所有帮助AP preciated!

修改

只是要清楚,这里有一些关于部分匹配的详细信息,这将是一个问题。

是的,我知道有些比赛会比其他人更棘手。在我的数组,也没有所谓的关键节目,但有一个叫开发这样的字符串 Python编程可以被放置于子键 Python开发因为这是开发商键的子键。

其他如字符串数据中心,因为有这个字数据中心<主键的几个子键这将是棘手/ code>。

让我们为例字符串计算机技术人员或技术人员的PC 。即不再在我的阵列的子密钥。但也有一个名为计算机技术人员子键以及 PC技术人员。哪一个部分匹配,如果在字符串中存在两个子键?

如果我能得到一些基本的东西,现在,将ATLEAST采取的一些部分匹配的照顾,那么我在正确的轨道上。

编辑2

下面是一个字符串业务分析师的另一个例子。有一个在我的阵列中的子键名为业务系统分析师。这个词商业分析师是子键这两个词。所以,我需要创造的东西,我可以继续添加到每一个新的字符串来在未数组中正确的匹配时间。

编辑3

想了几分钟后,我认为最好的办法将是手动添加进来一个数组,并引用那些我选择的子键的每个部分字符串。

OES有意义?

所以,我需要创建一个PHP代码片段,我不断加入到部分匹配的字符串指向数组中的关键阵列的容器。我该怎么做?


解决方案

我写了根据您的要求有点单个字的匹配。因此,网​​络将匹配网​​络的任何东西放在这里,因为它包含网​​络,等...我不知道这是否是你想要的,但也许它会帮助你。

 类PageWalker {
    公共$页= NULL;
    公共职能__construct($页){
        返回$这个 - &GT;页= $页;
    }    公共职能clean_str($ STR){
        回用strtolower(preg_replace('/ [-_ \\〜\\ @#\\ $ \\%\\ ^ \\&安培;!\\ * \\(\\)] /','',$ STR));
    }    公共职能get_results($输入= FALSE){
        $结果=阵列();
        的foreach($这个 - &GT;页为$页=&GT; $ ARR):
            的foreach($改编为$ I =&GT; $ V):
                如果(in_array($这个 - &GT; clean_str($输入),爆炸('',$这个 - &GT; clean_str($ I)))):
                    $结果[$ i] = $ V;
                万一;
            endforeach;
        endforeach;
        返回$结果;
    }
}$步行=新PageWalker($页);的print_r($步入式&GT; get_results('网络'));

这里的

的eval.in例子

I have this bit of PHP code which strips - the dash from the incoming string. The array key may or may not have a - between each word. So if the string has spaces or dashes, the string is still matched to the key.

Now I need to expand this a little to have a partial match locate the key in the array.

Here's what I have so far.

My array

$pages = array(

'Administrator' => array(
    'network-administrator' => array('title' => 'Network '.$li_1, 'description' => 'Network '.$li_1.' '.$temp_content, 'post' => '<p>Network '.$li_1.' '.$temp_content.'.</p>'),
    'database administrator' => array('title' => 'Database '.$li_1, 'description' => 'Database '.$li_1.' '.$temp_content, 'post' => '<p>Database '.$li_1.' '.$temp_content.'.</p>'),
),

'Analyst' => array(
    'business systems analyst' => array('title' => 'Business Systems '.$li_2, 'description' => 'Business Systems '.$li_2.' '.$temp_content, 'post' => '<p>Business Systems '.$li_2.' '.$temp_content.'.</p>'),
    'data-analyst' => array('title' => 'Data '.$li_2, 'description' => 'Data '.$li_2.' '.$temp_content, 'post' => '<p>Data '.$li_2.' '.$temp_content.'.</p>'),
),

);

PHP foreach loop

$t_keys = array();

foreach ($pages as $k => $arr2) {
    foreach (array_keys($arr2) as $a) {
        $new_key = str_replace("-", " ", $a);
        $t_keys[$new_key] = array( $k, $a );
    }
}
$str = str_replace(array('-', ' and '), ' ', strtolower($position));
if (array_key_exists($str, $t_keys)) {
    $target = $pages[ $t_keys[$str][0] ][ $t_keys[$str][1] ];
}

$t_keys[$str][0] accesses the key Administrator and $t_keys[$str][1] accesses sub-key network-administrator.

What I need to do is match incoming strings from the query string in the URL to find matches in the sub-keys of the array.

Example partial match strings that are coming in are oracle database administrator. That key is not in my array, but oracle administrator is, as well as database administrator.

So how could I match strings coming in to the sub-keys in the array if similar words exist?

Here is a small list of incoming partial match strings coming in my error log.

it 
apple 
css 
.net developer
desktop 
.net 
python 
apple 
data center 
phone technician
sql database developer
jquery
delphi
css
python
pc
software
xml
webmaster
research development
python programming
computer technician or pc technician
.net
c  

in some cases, the above partial matches don't exist anywhere in the array. I am just doing a redirect on those. But others such as data center and python programming just as an example are in the array.

Example for the string data center, the nearest match in the array would be data center technician and the nearest match for string python programming would be python developer.

All help appreciated!

EDIT

Just to be clear, here is some more info about partial matching that will be an issue.

yes I know that some matches will be more trickier than others. In my array, there is no key called programming but there is a key called developer so string python programming could be placed to sub key python developer as that is a sub key of the developer key.

Others such as the string data center it will be trickier because there are several sub keys of main keys that have the word data center.

Let's take for example the string computer technician or pc technician. That is no longer a sub-key in my array. but there are sub keys called computer technician as well as pc technician. Which one to partial match if both sub keys exist in the string?

If I can get something basic for now that will atleast take care of some of these partial matches, then I'm on the right track.

EDIT 2

Here's another example of a string business analyst. There is a sub key in my array called business systems analyst. The word business and analyst are both words in the sub key. So I need to create something that I can continue to add onto every time a new string comes in that isn't matching properly in the array.

EDIT 3

After thinking for a few minutes, I think the best way will be to manually add each partial string that comes in to an array and reference those to the sub key of my choosing.

oes that make sense?

So I need to create a php snippet that I keep adding to the array container of partial match strings that point to the key in the array. How can I do that?

解决方案

I wrote a little single word match based on your requirements. So network will match against network whatever stuff goes here because it contains network, etc... I'm not sure if it's what you want, but maybe it'll help you.

class PageWalker{
    public $pages = null;
    public function __construct($pages){
        return $this->pages = $pages;
    }

    public function clean_str($str){
        return strtolower(preg_replace('/[-_\~\!@#\$\%\^\&\*\(\)]/', ' ', $str));
    }

    public function get_results($input=false){
        $results = array();
        foreach($this->pages as $page => $arr):
            foreach($arr as $i => $v):
                if(in_array($this->clean_str($input), explode(' ', $this->clean_str($i)))):
                    $results[$i] = $v;
                endif;
            endforeach;
        endforeach;
        return $results;
    }
}

$walk = new PageWalker($pages);

print_r($walk->get_results('network'));

Here's an eval.in example

这篇关于需要补充的字符串部分匹配查找在foreach循环查找数组键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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