I have a while loop where I am returning departments. I want to be able to determine if it is the first time a department is returned. The code is as follows:
$myArray = array('Finance', 'Marketing', 'Customer Service', 'Marketing', 'Marketing', 'IT', 'Finance', 'IT');
while(list ($key, $val) = each ($myArray))
{
echo $val . "<br />";
}
The above code outputs the below:
Finance
Marketing
Customer Service
Marketing
Marketing
IT
Finance
IT
Is there a way where I can have it output the following?:
Finance(first)
Marketing(first)
Customer Service(first)
Marketing
Marketing
IT(first)
Finance
IT
I think you will have to keep track of used values manually:
<?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;
}
}
You could do something like this:
$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']}";
}
}
Using a whileloop and check within the loop that will be made every_single_iteration using the following:-
$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++;
or
$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++;
try this: (short and sweet)
<?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/>';
}
So, all you need to do is to add .(isset($t[$val]) ? '' : $t[$val] = '(first)').
with your echo within your existing code.
I you want it to be with a while loop, I guess you could try something like this
$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++;
}
Personally I would use a foreach like @Parziphal suggested