我有两个阵列。第一个是多维数组。第二个数组包含键和值对。现在我的目标是我想从第一个数组值检查每个数组的second index
,以检查第二个数组中的值是否存在?如果是,则第一个数组值需要替换为第二个数组值。
$first_array = Array
(
[0] => Array
(
[0] => 2012/12
[1] =>
[2] => "SI"
[3] =>
[4] =>
[5] =>
)
[1] => Array
(
[0] => 2012/12
[1] =>
[2] => "MB"
[3] =>
[4] =>
[5] =>
)
)
$second_array = array(
["MB"] => "WE",
["SI"] => "SA",
["SO"] => "SA",
)
我的输出应该是这样的
$first_array = Array
(
[0] => Array
(
[0] => 2012/12
[1] =>
[2] => "SA"
[3] =>
[4] =>
[5] =>
)
[1] => Array
(
[0] => 2012/12
[1] =>
[2] => "WE"
[3] =>
[4] =>
[5] =>
)
)
答案 0 :(得分:0)
foreach怎么样?
foreach($first_array as &$v)
{
//$v[2] = isset($second_array[$v[2]]) ? $second_array[$v[2]] : $v[2];
if(isset($second_array[$v[2]))
$v[2] = $second_array[$v[2]];
}
print_r($first_array);
答案 1 :(得分:0)
使用简单的foreach
循环和if
<?php
ini_set('display_errors', 1);
$first_array = Array
(
0 => Array
(
0 => "2012/12",
1 => "",
2 => "SI",
3 => "",
4 => "",
5 => "",
),
1 => Array
(
0 => "2012/12",
1 => "",
2 => "MB",
3 => "",
4 => "",
5 => "",
)
);
$second_array = array(
"MB" => "WE",
"SI" => "SA",
"SO" => "SA",
);
foreach($first_array as $key1 => $data)
{
if(isset($second_array[$data[2]]))
{
$first_array[$key1][2]=$second_array[$data[2]];
}
}
print_r($first_array);