Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I have been trying to loop nested foreach loops but the problem is first foreach loop records repeating as the count of second foreach loop first array is coming from mysql data and second array I have wrote below, In my case i want to loop the color presets in second array with first foreach loop results. I'm not much good in arrays please help me to solve this issue. here is the second array and code :

$colors = array ( 
        0 => array ("id"=> 0, "dark" => "#16a085", "light" => "#1ABC9C"),
        1 => array ("id"=> 1, "dark" => "#2980B9", "light" => "#3498DB "),
 );
$unique = array_unique($colors, SORT_REGULAR);
          foreach ($skill as $skilldata) {
                 foreach ($unique as $key => $val) {

  <div class="skillbar clearfix " data-percent="<?php echo $skilldata['js_skill_perc'].'%'; ?>">
    <div class="skillbar-title" style="background: <?php echo $val['dark']?>;">
    <span><?php echo $skilldata['js_skill_title']; ?></span></div>
    <div class="skillbar-bar" style="background-color: <?php echo $val['light']?>; width: <?php echo $skilldata['js_skill_perc'].'%'; ?>;"></div>
    <div class="skill-bar-percent"><?php echo $skilldata['js_skill_perc'].'%'; ?></div>
 </div>

<?php }} ?>

Output should be like : HTML5 (green) PHP(blue) and SEO (green) but this is how output looks like:

enter image description here

share|improve this question
    
What is the content of $skill? Your question is not very clear... What is the output you wanted? – Alan Machado Oct 2 '15 at 11:00
    
as shown as image that's comming from mysql data(HTML, PHP, SEO) – Sahan Perera Oct 2 '15 at 11:01
    
This question is not too clear to me also. Can you show me the expected output? Do you want HTML5 (green) PHP(blue) and SEO (green)? – lolka_bolka Oct 2 '15 at 11:10
    
@lolka_bolka yeah exactly thats what I want and by the way sorry about my English – Sahan Perera Oct 2 '15 at 11:12
    
@AlanMachado I updated the question – Sahan Perera Oct 2 '15 at 11:16
up vote 2 down vote accepted

If You need to just switch the colors from line to line, You can use CSS for this (see :nth-child(even) and :nth-child(odd)) or do it in PHP like this:

$colors = array(
    ...
);
$colors_count = count($colors);
$colors_index = 0;
foreach ($skill as $skilldata) {

    $color = $colors[$colors_index % $colors_count];
    $colors_index++;

    echo ... whatever using $color ...
}
share|improve this answer
    
That is cool, its working – Sahan Perera Oct 2 '15 at 11:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.