php random element from array no siblings -
trying item randomly array($colors
) without having 2 same colors next each other.
<div class="list"> <?php foreach ($team $member): $index++; ?> <div class="member location-<?php echo strtolower($member->location); ?>"> <a style="background: #fff url('<?php echo $member->profileimage; ?>') no-repeat;" data-start-date="<?php echo $member->startdate; ?>"> <?php shuffle($colors); // shuffle array ?> <span class="name" style="background-color: #<?php echo array_pop($colors)->color; ?>"><?php echo $member->name; ?></span> <span class="job-title"><span class="text"><?php echo $member->jobtitle; ?></span></span> </a> </div> <?php endforeach; ?> </div>
right have cases when i'm getting color next each other.
how each time different color? i'd mention count($team) > count($colors)
(is greater).
update:
$colors looks (i've printed 3 items, count higher)
array(18) { [0]=> object(stdclass)#112 (1) { ["color"]=> string(6) "5ebedb" } [1]=> object(stdclass)#111 (1) { ["color"]=> string(6) "c75d40" } [2]=> object(stdclass)#110 (1) { ["color"]=> string(6) "faaf37" } }
here how can it. striped html tags make solution more understandable.
$colors = [...]; // see definition of $color array $colorcount = count($colors); $lastcolorid = null; foreach ($team $member) { // choose random color $colorid = rand(0,$colorcount-1); if ($lastcolorid == $colorid) { // take next color if it's same previous 1 $colorid = ($colorid + 1) % $colorcount; } $lastcolorid = $colorid; // color $color = $colors[$colorid]->color; }
Comments
Post a Comment