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

时间:2014-09-27 18:40:59

标签: php arrays regex string array-key

我需要找到一种方法将字符串部分匹配到PHP数组中的子键。

示例:

string = howdy-doody show正如您所看到的那样,-短划线和单词之间有空格。在我的PHP数组中,子键可能是howdy doody show,没有破折号,或者可能是howdy doody-show,短划线位于字符串中的不同单词之间。

如何在给定字符串的数组中找到子键?

示例数组

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

);

示例字符串

network administrator

用于定位数组值的示例变量

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

使用上面的变量,它将无法在数组中找到子键,因为子键使用-短划线,如network-administrator

那么如何获取包含原始子键的数组值并使用具有空格的字符串返回其内容,而不是像network administrator那样使用破折号?

非常感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

这是一个使用递归的函数,并使用新密钥获取传递给它的数组:

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;
}

示例:

首先需要获取新的数组,在这种情况下,我们想要更改密钥:&#39; _&#39;和&#39; - &#39;到太空:

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

然后使用我们的新数组:

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

您案例中的示例:

<?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;

<强>输出

Network 

答案 1 :(得分:1)

这是一种方法:将原始密钥重新映射到包含剥离密钥的新数组,并将原始密钥存储在数组的值中。

$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 );
    }
}

来自$t_keys的示例键值对:

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

要访问原始数组中的值,我们需要获取$pages['Administrator']['network-administrator'],或使用$t_keys$pages[ $t_keys['network administrator'][0] ][ $t_keys['network administrator'][1] ]中的等效值。

与您的搜索字符串匹配:

$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'];
}

如果您不需要知道$pages中的密钥是什么(例如“管理员”和“网络管理员”)并且只想直接查看相关数据,则可以创建引用而不是存储密钥。这是一个例子:

$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];
    }
}

现在$refs['network administrator']就像是$pages['Administrator']['network-administrator']的捷径; $refs['network administrator']['post']访问$pages['Administrator']['network-administrator']['post'],依此类推。