PHP - 当你找到带有 array_search 的项目时,如何获取数组值(如 [0]、[1]、[2])
PHP - How to get the array value (like [0], [1], [2]) when you find the item with array_search
我正在尝试尽可能具体,但我要走了。
我有 2 个数组,f.e.:
$foo = array("a", "b", "c", "d", "e");
$fee = array("one", "two", "three", "four", "five");
'one' 匹配 'a','two' 匹配 'b','three' 匹配 'c' 等
假设我在文本输入中键入 "abc"
。如何获得 "onetwothree"
作为输出?
我在想。如果我能得到输入文本的数组值,我就可以用它来找到我想要的数组项。
如果这没有意义,我很抱歉,但是对于那些理解的人,我很感激帮助。
更新(示例):
输入:'a'
输出:'one'
试图弄清楚:
在这种情况下 'a'
的键值是 [1].
我想知道当我用 array_search
搜索 'a'
时如何使用命令或任何东西来获取该值。也许我正在使用更复杂的方式......欢迎任何更快地完成此操作的建议! :)
$foo = array('a', 'b', 'c', 'd', 'e');
$fee = array('one', 'two', 'three', 'four', 'five');
$output = '';
$text = 'abc';
- 我们可以将文本拆分成一个数组,每个字符作为一个元素。
- 搜索索引
- 确保索引也存在于另一个数组中
- 使用索引追加匹配的字符串。
foreach(str_split($text) as $char) {
$index = array_search($char, $foo);
if($index !== false && isset($fee[$index])) $output .= $fee[$index];
}
echo $output;
onetwothree
只需获取您正在输入的 $foo
数组的索引,然后从 $fee
数组中获取值并连接它们。
我正在尝试尽可能具体,但我要走了。
我有 2 个数组,f.e.:
$foo = array("a", "b", "c", "d", "e");
$fee = array("one", "two", "three", "four", "five");
'one' 匹配 'a','two' 匹配 'b','three' 匹配 'c' 等
假设我在文本输入中键入 "abc"
。如何获得 "onetwothree"
作为输出?
我在想。如果我能得到输入文本的数组值,我就可以用它来找到我想要的数组项。
如果这没有意义,我很抱歉,但是对于那些理解的人,我很感激帮助。
更新(示例):
输入:'a'
输出:'one'
试图弄清楚:
在这种情况下 'a'
的键值是 [1].
我想知道当我用 array_search
搜索 'a'
时如何使用命令或任何东西来获取该值。也许我正在使用更复杂的方式......欢迎任何更快地完成此操作的建议! :)
$foo = array('a', 'b', 'c', 'd', 'e');
$fee = array('one', 'two', 'three', 'four', 'five');
$output = '';
$text = 'abc';
- 我们可以将文本拆分成一个数组,每个字符作为一个元素。
- 搜索索引
- 确保索引也存在于另一个数组中
- 使用索引追加匹配的字符串。
foreach(str_split($text) as $char) {
$index = array_search($char, $foo);
if($index !== false && isset($fee[$index])) $output .= $fee[$index];
}
echo $output;
onetwothree
只需获取您正在输入的 $foo
数组的索引,然后从 $fee
数组中获取值并连接它们。