douqiang1851 2014-09-27 18:40
浏览 66
已采纳

将字符串与php数组部分匹配中的子键值进行比较

I need to figure out a way to partial match a string to a sub key in my PHP array.

example:

string = howdy-doody show as you can see there is a - dash and a space between the words. In my PHP array the sub key might be howdy doody show with no dashes or it might be howdy doody-show with the dash between a different word in the string.

How can I find the sub key in the array with the string given?

sample 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>'),
),

);

sample string

network administrator

sample variable to locate the array value

$content = $pages['Administrator']['network administrator'];

with the above variable it won't find the sub key in the array because the sub key uses a - dash like this network-administrator.

So how would I get the array value including the original subkey and returns its contents using the string that has the space instead of dash like so, network administrator?

Much appreciated for help!

  • 写回答

2条回答 默认 最新

  • doubi5127 2014-09-27 19:03
    关注

    Here's one way to do it: remap your original keys to a new array containing stripped keys, and store the original key in the value for the array.

    $t_keys = array();
    
    foreach ($pages as $k => $arr2) {
        foreach (array_keys($arr2) as $a) {
            // perform whatever transformations you want on the key here
            $new_key = str_replace("-", " ", $a);
            // use the transformed string as the array key;
            // we still need to access the data in the original array, so store the outer
            // array key ("Administrator", "Analyst", etc.) and the inner array key
            // ("network-administrator", etc.) in a subarray.
            $t_keys[$new_key] = array( $k, $a );
        }
    }
    

    An example key-value pair from $t_keys:

    $t_keys['network administrator'] = ['Administrator', 'network-administrator']
    

    To access the value in the original array, we need to get $pages['Administrator']['network-administrator'], or, using the equivalent values from $t_keys: $pages[ $t_keys['network administrator'][0] ][ $t_keys['network administrator'][1] ].

    To match against your search string:

    $str = str_replace("-", " ", $original_search_string_from_url);
    
    // check if it's in the transformed keys array
    if (array_key_exists($str, $t_keys)) {
        // now we can access the data from our $pages array!
        $target = $pages[ $t_keys[$str][0] ][ $t_keys[$str][1] ];
        echo "the proper name for $str is " . $t_keys[$str][1] . "
    ";
    //  output: "the proper name for network administrator is network-administrator"
    
        // access various attributes of the network-administrator:
        echo "the title of $str is " . $target['title'];
    }
    

    If you don't need to know what the keys in $pages are (e.g. 'Administrator' and 'network-administrator') and just want to get straight to the relevant data, you could create references instead of storing the keys. Here's an example:

    $refs = array();
    
    foreach ($pages as $k => $arr2) {
        foreach (array_keys($arr2) as $a) {
            // perform whatever transformations you want on the key here
            $new_key = str_replace("-", " ", $a);
            // create a reference ( =& ) to the contents of $pages[$k][$a]
            // $refs[$new_key] points directly at $pages[$k][$a]
            $refs[$new_key] =& $pages[$k][$a];
        }
    }
    

    Now $refs['network administrator'] acts like a shortcut to $pages['Administrator']['network-administrator']; $refs['network administrator']['post'] accesses $pages['Administrator']['network-administrator']['post'], and so on.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入