Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中,有没有一种方法可以确定在while循环中首次使用值?_Php_While Loop_Iteration - Fatal编程技术网

在PHP中,有没有一种方法可以确定在while循环中首次使用值?

在PHP中,有没有一种方法可以确定在while循环中首次使用值?,php,while-loop,iteration,Php,While Loop,Iteration,我有一个while循环,在那里我返回部门。我希望能够确定这是否是第一次返回部门。代码如下: $myArray = array('Finance', 'Marketing', 'Customer Service', 'Marketing', 'Marketing', 'IT', 'Finance', 'IT'); while(list ($key, $val) = each ($myArray)) { echo $val . "<br />"; } $myArray=arra

我有一个while循环,在那里我返回部门。我希望能够确定这是否是第一次返回部门。代码如下:

$myArray = array('Finance', 'Marketing', 'Customer Service', 'Marketing', 'Marketing', 'IT', 'Finance', 'IT');
while(list ($key, $val) = each ($myArray))
{
    echo $val . "<br />";
}
$myArray=array('Finance'、'Marketing'、'Customer Service'、'Marketing'、'Marketing'、'IT'、'Finance'、'IT');
while(list($key,$val)=每个($myArray))
{
echo$val.“
”; }
上述代码输出以下内容:

财务

市场营销

客户服务

市场营销

市场营销

财务

有没有办法让它输出以下内容

金融(第一)

市场营销(第一)

客户服务(第一)

市场营销

市场营销

IT(第一)

财务


我认为您必须手动跟踪使用的值:

<?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;
    }
}

您可以这样做:

$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']}";
    }
}
$varList[0]['value']='Finance';
$varList[1]['value']='Marketing';
$varList[2]['value']='Customer Service';

对于($i=0;$i,使用whileloop并在循环内进行检查,该循环将在每次\u单次\u迭代中使用以下内容:-

$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');
$numItems=count($myArray);
$i=0;
$firstitem=$myArray[0];
$i++;
而($i试试这个:(又短又甜)


如果你想让它有一个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++;
}
$myArray=array('Finance'、'Marketing'、'Customer Service'、'Marketing'、'Marketing'、'IT'、'Finance'、'IT');
$used=[];
$numItems=count($myArray);
$i=0;

虽然($iif$varList有一百万个项目,您运行了一百万次,但您只需要在提供了关于完成什么或多久完成一次的scope时才需要它。我只提供了如何使其工作,最佳实践由他来确定:)您在此处使用字符串插值是危险的。您在HTML上下文中输出任意数据。您应该使用
htmlspecialchars()
。不,我向他展示了如何迭代值,并为他提供了一个调试媒介。不是由我来为他解决每一个细节,我是来回答这个问题的。对于建议编辑的@aconnely,
empty($used[$department])
不会触发任何无效的索引错误。事实上,您可以将empty与不存在的数组和索引一起使用,而不会发生任何情况。isset和empty之间的唯一区别是isset表示“不为null”,而empty表示“不等于null”(即空字符串、false、0等)但是,
isset
在本例中更自然地读取为“if key is set…”,而不是“if key not empty…”。当我使用已知数组长度尝试时,这非常有效。如果while循环在SQL记录中循环,我可以这样做吗?@sjw0525是的,它应该可以处理SQL查询结果,我不明白为什么它不能
<?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/>';
    }
$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++;
}