Kalid
Kalid

Reputation: 157

Combined array is not working in php

My Array:

Array Set One:

$values = array( "Paid Search Ads" => "$avg_2_1",
               "TV" => "$avg_2_2" , 
               "Radio" => "$avg_2_3",
               "Print Ads" => "$avg_2_4",
               "Social Media Posts" => "$avg_2_5",
               "Facebook Ads" => "$avg_2_6",               
               "LinkedIn Ads" => "$avg_2_7",
               "Twitter Ads" => "$avg_2_8",
               "YouTube" => "$avg_2_9",
               "Vimeo" => "$avg_2_10",
               "Email Marketing" => "$avg_2_11",
               "Email Newsletter" => "$avg_2_12",
               "Pay-per-click Ads" => "$avg_2_13",
               "Blogs" => "$avg_2_14",
               "eBooks" => "$avg_2_15",
               "Online Videos" => "$avg_2_16",
               "Video Ads" => "$avg_2_17",
               "Other" => "$avg_2_18",
               "None of these" => "$avg_2_19"
              );

$values = array_unique($values);  
arsort($values);  
$values = array_slice($values, 0, 5);

Array Set 2:

$color_array = array(             "color1" => "#f0f0f0",
       "color2" => "#c5c5c5", 
       "color3" => "#000000",
       "color4" => "#565656",
       "color5" => "#242423"          );

Out Put Section:

<?php 
      foreach ($values as $key => $val) {
           //echo "$key = $val\n";
           foreach($color_array as $col => $colcode){
                echo'<tr><td>'.$val.'%</td>
                <td class="barWidth vMiddle">
                <div class="Hzbar" style="width:'.$val.'% !important; background:'.$colcode.'"></div>
                </td>
                <td>'.$key.'Paid Search Ads</td>
                </tr>';
           }
      }
      ?>

Current Output:

![enter image description here][1]

But Expected output is ONLY 5 RESULT with 5 different colors :(

EXPECTED OUTPUT

enter image description here

Upvotes: 0

Views: 52

Answers (4)

Jonathan Crowe
Jonathan Crowe

Reputation: 5803

Can you change your array format to contain all the data you want? ie:

$values = array(
    "Paid Search Ads" => array(
       'width' => "$avg_2_1", 
       'color' => 'color1'
    ),
    "TV" => array(
        'width' => "$avg_2_2", 
        'color' => 'color2'
   ), 
    etc..
);

Then in your foreach:

foreach ($values as $key => $val) {                           

    echo'<tr>
            <td>'.$val['width'].'%</td>
            <td class="barWidth vMiddle">
                <div class="Hzbar" style="width:'.$val['width'].'% !important; background:'.$color_array[$val['color'].'"></div>
            </td>
            <td>'.$key.'Paid Search Ads</td>
         </tr>';
 }     

Upvotes: 1

prava
prava

Reputation: 3986

Try this one.

<?php
foreach ($values as $key => $val) {
    foreach($color_array as $col => $colcode){
        echo'                                
            <tr>
                <td>'.$val.'%</td>
                <td class="barWidth vMiddle">
                    <div class="Hzbar" style="width:'.$val.'% !important; background:'.$colcode.'"></div>
                </td>
                <td>'.$key.'Paid Search Ads</td>
            </tr>
        ' ;

        unset($color_array[$col]);
        break;
    }
}
?>

Note: This is the solution if we assign the first 5 records of the first array with each of the equal order of the second array.

Upvotes: 1

Oli Soproni B.
Oli Soproni B.

Reputation: 2800

Have you check the the array $values output

for example,

$values = array(
"Paid Search Ads" => "sample",
   "TV" => "sample" , 
   "Radio" => "sample",
   "Print Ads" => "colin",
   "Social Media Posts" => "colin",
   "Facebook Ads" => "colin",               
   "LinkedIn Ads" => "colin",
   "Twitter Ads" => "sample",
   "YouTube" => "sample2",
   "Vimeo" => "sample2",
   "Email Marketing" => "sample2",
   "Email Newsletter" => "sample2",
   "Pay-per-click Ads" => "sample",
   "Blogs" => "sample3",
   "eBooks" => "sample",
   "Online Videos" => "sample3",
   "Video Ads" => "sample3",
   "Other" => "sample",
   "None of these" => "sample"
);

print_r($values);

$values = array_unique($values);

print_r($values);

the first output print

will print the entire array

the second output print

will print the unique filtered array

check php documentation

http://php.net/manual/en/function.array-unique.php

hope it would hope you..

Upvotes: 1

user4447894
user4447894

Reputation:

Try this:

$vk=0;
    foreach($values  as $val) { //echo "$key = $val\n";
        $vk++;

        $ck=0;
        foreach($color_array as $col => $colcode){
            $ck++;
            if( $vk==$ck){
                echo'                                
                <tr>
                <td>'.$val.'%</td>
                <td class="barWidth vMiddle">
                <div class="Hzbar" style="width:'.$val.'% !important; background:'.$colcode.'"></div>
                </td>
                <td>'.$key.'Paid Search Ads</td>
                </tr>
                ' ;
            }
        }
    }

Upvotes: 2

Related Questions