Per the php.net documentation for foreach:
There are two syntaxes:
foreach (array_expression as $value)
statement
foreach (array_expression as $key => $value)
statement
1
Use the second syntax to have the index assigned automatically to $key. Then use the modulo arithmetic operator to compute the remainder of the value of $index divided by the length of the array of class names (i.e. $index % count($classarr)).
0 % 4 => 0
1 % 4 => 1
2 % 4 => 2
3 % 4 => 3
4 % 4 => 0
5 % 4 => 1
...
That way the index into $classarr will be sequential and repeat, resulting in the class names being repeated.
<?php
$sql="select * from category order by id desc";
$catdata=$dbobj->db_get_data($sql);
$classarr = array("item-exhibitation maploc", "item-parking maploc", "item-offices maploc", "item-storage maploc");
foreach ($catdata as $index => $v) {
$class = $classarr[$index % count($classarr)];
?>
<div class="col-sm-3">
<a href="javascript:void(0)" class="<?php echo $class;?>"><?php echo $v['name'] ?></a><!-- activemap1 -->
</div>
<?php
}
?>
See a demonstration of this in this PHPfiddle. The output can be seen in the snippet below:
#Legend {
float: right;
}
.item-exhibitation {
color: #ff0000;
}
.item-parking {
color: #00f;
}
.item-offices {
color: #0f0;
}
.item-storage {
color: #888;
}
<div id="Legend">
<h3>Legend</h3>
<div class="item-exhibitation">.item-exhibitation</div>
<div class="item-parking">.item-parking</div>
<div class="item-offices">.item-offices</div>
<div class="item-storage">.item-storage</div>
</div>
<h3>Items</h3>
<div class="col-sm-3"><a href="javascript:void(0)" class="item-exhibitation maploc">Exposition Center</a>
<!-- activemap1 -->
</div>
<div class="col-sm-3"><a href="javascript:void(0)" class="item-parking maploc">Parking Lot A</a>
<!-- activemap1 -->
</div>
<div class="col-sm-3"><a href="javascript:void(0)" class="item-offices maploc">Insurance Agency A</a>
<!-- activemap1 -->
</div>
<div class="col-sm-3"><a href="javascript:void(0)" class="item-storage maploc">Personal Storage A</a>
<!-- activemap1 -->
</div>
<div class="col-sm-3"><a href="javascript:void(0)" class="item-exhibitation maploc">Convention Center</a>
<!-- activemap1 -->
</div>
<div class="col-sm-3"><a href="javascript:void(0)" class="item-parking maploc">Parking Lot B</a>
<!-- activemap1 -->
</div>
1http://php.net/foreach
class="item-exhibitation maploc"will change accordingly.