这是示例代码:
$array1 = array("Name1", "Name2", "Name3");
$array2 = array("Name2" => "NameX");
foreach($array1 as $val)
{
echo $val."<br/>";
}
这将输出: Name1 Name2 Name3
我怎样才能输出这个: Name1 NameX Name3
Yogesh Suthar提交了正确的回复:
$array1 = array("Name1", "Name2", "Name3");
$array2 = array("Name2" => "NameX");
foreach($array1 as $val) {
if (array_key_exists($val, $array2)) {
echo $array2[$val];
}
else {
echo $val."<br/>";
}
}