arrays with foreach
Posted: Thu Jun 02, 2005 1:23 pm
how can you tell if you are on the last loop of a foreach? is this possible?
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Code: Select all
$a_val = array("one","two","three");
$arrayCount = count($a_val);
$counter = 0;
foreach($a_val as $ak => $val){
$counter++;
if($counter+1 == $arrayCount)
echo 'Last Item';
}Code: Select all
$array = array("one","two","three");
$count = count($array);
for ($x=1;$x<=$count;$x++) {
if ($count == $x) {
echo 'this is the last iteration';
}
echo '$x = '. $x .'<br>';
}Code: Select all
$count = count($qw);
$counter = 0;
foreach ($qw as $key => $value)
{
$counter++;
// query WHERE
if (isset($value['select']))
{
$ty = 'select';
}
else if (isset($value['text']))
{
$ty = 'text';
}
$and_or = ($value['and_or'] == '1' ? 'AND' : 'OR');
if ($counter == $count)
{
unset($and_or);
}
$qw_map[] = $query_map['qw'][$value['qw_id']]['field_name'].' '.$operators[$value['operator_id']].' \''.$value[$ty].'\''.' '.$and_or.'';
// query FROM
$qf_map[$query_map['qw'][$value['qw_id']]['fk_qf_id']] = $query_map['qf'][$query_map['qw'][$value['qw_id']]['fk_qf_id']];
}