在PHP数组部分匹配比较两个字符串子键值 [英] compare string to sub key value in php array partial match
问题描述
我需要一个方式串弄明白部分匹配到我的PHP数组中的子键。
I need to figure out a way to partial match a string to a sub key in my PHP array.
例如:
字符串=你好,杜迪节目
,你可以看到有一个 -
破折号和词之间有一个空格。在我的PHP数组的子键可能是你好杜迪秀
没有破折号或它可能是你好杜迪秀
与在字符串中有不同的定义之间的冲刺。
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?
样品阵列
$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
样品变量定位数组值
sample variable to locate the array value
$content = $pages['Administrator']['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
?
大部分AP preciated的帮助!
Much appreciated for help!
推荐答案
下面是做到这一点的一种方法:重新映射原来的键包含剥离键一个新的数组,并存储在数组中值的原始密钥。 p>
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 );
}
}
这是一个例子键值对 $ t_keys
:
$t_keys['network administrator'] = ['Administrator', 'network-administrator']
要在原来的访问数组中的价值,我们需要获得 $页['管理员'] ['网络管理员']
,或者使用等效值从 $ t_keys
: $页[$ t_keys [网络管理员] [0] [$ t_keys [网络管理员] [1] ]
。
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] ]
.
要匹配搜索字符串:
$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.
这篇关于在PHP数组部分匹配比较两个字符串子键值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!