compare string to sub key value in php array partial match

醉酒当歌 提交于 2019-12-11 22:42:06

问题


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!


回答1:


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] . "\n";
//  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.




回答2:


Here is a function that use recursion and get you the array you passed to it but with new keys:

function getArr($arr, $reg, $char)
{
    foreach ($arr as $key => $value) {
        if (is_array($value))
            $newArray[preg_replace($reg, $char, $key)] = getArr($value, $reg, $char);
        else
            $newArray[preg_replace($reg, $char, $key)] = $value;
    }
    return $newArray;
}

Example:

You first need to get your new Array, in this case we would like to change in keys: '_' and '-' to space:

$newPages = getArr($pages, '/_|-/', ' ');

and then use our new array:

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

Example in your case:

<?php
function getArr($arr, $reg, $char)
{
    foreach ($arr as $key => $value) {
        if (is_array($value))
            $newArray[preg_replace($reg, $char, $key)] = getArr($value, $reg, $char);
        else
            $newArray[preg_replace($reg, $char, $key)] = $value;
    }
    return $newArray;
}
$pages = array(

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

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

);

$content = getArr($pages, '/_|-/', ' ')['Administrator']['network administrator']['title'];
echo $content;

OUTPUT

Network 


来源:https://stackoverflow.com/questions/26077917/compare-string-to-sub-key-value-in-php-array-partial-match

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!