组合数组在php中不起作用

时间:2015-01-13 05:40:36

标签: php html arrays

我的阵列:

阵列集一:

$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);

数组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>';
           }
      }
      ?>

当前输出:

![在此处输入图片说明] [1]

但是预期的输出只有5种结果,有5种不同的颜色:(

预期输出

enter image description here

4 个答案:

答案 0 :(得分:2)

试试这个:

$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>
                ' ;
            }
        }
    }

答案 1 :(得分:1)

试试这个。

<?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;
    }
}
?>

注意:如果我们为第一个数组的前5个记录分配第二个数组的相等顺序,这就是解决方案。

答案 2 :(得分:1)

您是否检查了数组$ values output

例如,

$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);

第一个输出打印

将打印整个数组

第二个输出打印

将打印唯一的已过滤数组

检查php文档

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

希望它会希望你......

答案 3 :(得分:1)

您可以更改阵列格式以包含所需的所有数据吗?即:

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

然后在你的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>';
 }