PHP - 怎么搭配2台阵列 [英] PHP - how match 2 sets of arrays
本文介绍了PHP - 怎么搭配2台阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我建立使用PHP一个JSON文件,我想匹配$ ARRAY2与数组1的ID,并显示ARRAY1的ID的匹配值。我该怎么办呢?请参阅下面的code:
I am building a JSON file using php, and I want to match the IDs of $Array2 with Array1, and display the matched value of the IDs of array1. How can I do that? Please see the code below:
$array1 = array(
'1' => 'a',
'2' => 'b',
'3' => 'c',
'4' => 'd'
);
$string = "1,3|2,3|1,4";
$array2 = explode('|', $string);
$foo = '';
foreach ($array2 as $item) {
$foo .= '{' . $item . '},';
}
echo $foo;
结果显示 - {1,3},{2,3},{1,4},
,但
我想要得到的结果是 {A,C},{B,C},{A,D}
非常感谢。
推荐答案
我想这是它:
foreach ($array2 as $items) {
$values = implode(',', array_map(function($i) use ($array1) {
return $array1[$i];
}, explode(',', $items)));
$foo .= "{" . $values. "},";
}
在 array_map
函数从翻译在
。 $数组2
的号码对应的值$数组1
The function in array_map
translates the numbers in $array2
to the corresponding values from $array1
.
这篇关于PHP - 怎么搭配2台阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文