-2

我有点陷入困境,代码比这大得多,但这大致就是这样......

<?php
$other = 'white';

$array = array('red', 'blue', 'red', 'red',  'red');

foreach($array[1] as $match) //OR $match = $other;
{
    //Core Area
    if($match == 'red') { echo 'RED!'; }
    if($match == 'blue') { echo 'BLUE!'; }
    if($match == 'white') { echo 'white!'; }
}
?>

像现在这样,$other 不碍事就无法进入核心区域foreach。另一种方法是克隆——通过复制粘贴——到另一个地方。...这不会很好地工作...我尝试将该区域放在一个函数中,但没有许多全局值,它似乎不是一个可行的选择。有什么办法可以在foreach和之间切换=吗?

4

2 回答 2

1
$array[] = $other;

现在$other在数组中,因此它将在您在循环中比较的内容列表中。

为什么你想要这个或你真正要问的是飞过我的头。

于 2011-08-05T03:13:59.150 回答
0
<?php
$other = 'white';

$array = array('red', 'blue', 'red', 'red',  'red');

array_push($array, $other);    
foreach($array as $match) //OR $match = $other;
{ 
    //Core Area
  if($match == 'red') { echo 'RED!'; }
  if($match == 'blue') { echo 'BLUE!'; }
  if($match == 'white') { echo 'white!'; }
}

array_pop($array);

?>

或者:

<?php
$other = 'white';

$array = array('red', 'blue', 'red', 'red',  'red');

foreach($array as $match) //OR $match = $other;
{ 
    //Core Area
    custom_match($match);
}

custom_match($other);

function custom_match($color) {
  if($match == 'red') { echo 'RED!'; }
  if($match == 'blue') { echo 'BLUE!'; }
  if($match == 'white') { echo 'white!'; }
}
?>
于 2011-08-05T03:35:43.883 回答