我有以下数组,长度和值都是动态的
$one = array('11','12');
$two = array('0','0','0','0','1','1');
想要结果
$result = array('11','11','11','11','12','12');
答案 0 :(得分:2)
foreach ($two as $key => $value)
{
$result[$key] = $one[$value];
}
答案 1 :(得分:0)
我们正在使用array_map
,array_unique
和preg_replace
来表达所需的输出。
<?php
$one = array('11','12');
$two = array('0','0','0','0','1','1');
$search=array_map(function($value){
return $value='/\b'.$value."\b/";
},array_unique($two)); //Here we are creating an array which we want to search
print_r(preg_replace($search, $one, $two));
<强>输出:强>
Array
(
[0] => 11
[1] => 11
[2] => 11
[3] => 11
[4] => 12
[5] => 12
)
答案 2 :(得分:0)
我这样做:
<?php
$one = array('11','12');
$two = array('0','0','0','0','1','1');
$result = array();
foreach($two as $i){
$result[] = $one[(int) $i];
}
print_r($result);
?>
答案 3 :(得分:0)
Sahil的回答太低效了。使用更少的代码可以更有效地实现所需的输出。
输入:
$one = array('11','12');
$two = array('0','0','0','0','1','1');
方法#1:array_map()
$result=array_map(function($v)use($one){return $one[$v];},$two));
var_export($result);
方法#2:array_walk()
array_walk($two,function($v)use($one,&$result){$result[]=$one[$v];});
var_export($result);
方法#3:foreach()
foreach($two as $v){
$result[]=$one[$v];
}
var_export($result);
输出(无论方法如何):
array (
0 => '11',
1 => '11',
2 => '11',
3 => '11',
4 => '12',
5 => '12',
)