我有一个while循环,我正在返回部门。我希望能够确定这是否是第一次退回部门。代码如下:
$myArray = array('Finance', 'Marketing', 'Customer Service', 'Marketing', 'Marketing', 'IT', 'Finance', 'IT');
while(list ($key, $val) = each ($myArray))
{
echo $val . "<br />";
}
以上代码输出如下:
金融
营销
客户服务
营销
营销
IT
金融
IT
有没有办法让我输出以下内容?:
金融(第一)
营销(第一)
客户服务(第一)
营销
营销
IT(第一)
金融
IT
答案 0 :(得分:1)
我认为您必须手动跟踪使用的值:
<?php
$used = [];
foreach ($myArray as $department) {
if (isset($used[$department])) {
// The key exists, it means it's been used already
echo $department . '<br />';
} else {
// The key doesn't exist; we're using it for the first time
echo $department . ' (first)<br />';
// Add the key with a true value
$used[$department] = true;
}
}
答案 1 :(得分:0)
你可以这样做:
$varList[0]['value'] = 'Finance';
$varList[1]['value'] = 'Marketing';
$varList[2]['value'] = 'Customer Service';
for ($i=0;$i<count($varList);$i++) {
if ($varList[$i]['lastUsed'] == '') {
$varList[$i]['lastUsed'] = $now;
echo "{$varList[$i]['value']} (first time being used)";
} else {
echo "{$varList[$i]['value']} was last used {$varList[$i]['lastUsed']}";
}
}
答案 2 :(得分:0)
使用while循环并在循环中检查将使用以下内容进行every_single_iteration: -
$myArray = array('Finance', 'Marketing', 'Customer Service', 'Marketing', 'Marketing', 'IT', 'Finance', 'IT');
$numItems = count($myArray);
$i=0;
$firstitem=$myArray[0];
$i++;
while($i<$numItems-1){
$some_item=$myArray[$i];
$i++;
}
$last_item=$myArray[$i];
$i++;
或
$myArray = array('Finance', 'Marketing', 'Customer Service', 'Marketing', 'Marketing', 'IT', 'Finance', 'IT');
$keys = array_keys($myArray);
$numItems = count($keys);
$i=0;
$firstItem=$myArray[$keys[0]];
# Special handling of the first item goes here
$i++;
while($i<$numItems-1){
$item=$myArray[$keys[$i]];
# Handling of regular items
$i++;
}
$lastItem=$myArray[$keys[$i]];
# Special handling of the last item goes here
$i++;
答案 3 :(得分:0)
试试这个:(短而甜)
Target target = LogManager.Configuration.FindTargetByName(targetName);
while ((target != null) && (target is WrapperTargetBase))
{
target = (target as WrapperTargetBase).WrappedTarget;
}
因此,您需要做的就是在现有代码中添加带有回声的<?php
$myArray = array('Finance', 'Marketing', 'Customer Service', 'Marketing',
'Marketing', 'IT', 'Finance', 'IT');
while(list ($key, $val) = each ($myArray))
{
echo $val .(isset($t[$val]) ? '' : $t[$val] = '(first)').'<br/>';
}
。
答案 4 :(得分:0)
我希望它有一个while循环,我猜你可以试试这样的东西
$myArray = array('Finance', 'Marketing', 'Customer Service', 'Marketing', 'Marketing', 'IT', 'Finance', 'IT');
$used = [];
$numItems = count($myArray);
$i=0;
while($i<$numItems){
if (isset($used[$department])) {
echo $department;
} else {
echo $department . ' (first)';
$used[$department] = true;
}
$i++;
}
就个人而言,我会使用像@Parziphal建议的foreach