dongwu5318 2014-09-28 18:21
浏览 93
已采纳

需要在foreach循环中添加字符串部分匹配查找数组键

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?

  • 写回答

1条回答 默认 最新

  • dongshang6062 2014-09-28 18:50
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?