首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何针对散列数组中的特定键并输出不同的HTML?

如何针对散列数组中的特定键并输出不同的HTML?
EN

Stack Overflow用户
提问于 2018-06-17 01:07:14
回答 2查看 96关注 0票数 0

我在PHP中有一个散列数组,需要遍历它并提取细节,但也要针对一个特定的键并以不同的方式使用该信息。

我试图做的是在浏览器中显示每个用户的信息,但我希望IconColour不显示在屏幕上,而是构建一个图标,根据该值进行着色。

我用以下命令构建数组:

代码语言:javascript
复制
$results_array [] = array("Name"=>$fullname, "telephone"=>$telephone, "updated"=>$lastupdated, "LRNumber"=>$tacticalcallsign, "IconColour"=>$iconcolour);

要将此信息提取出来并显示到我正在使用的浏览器中:

代码语言:javascript
复制
foreach($results_array as $row => $value){
 echo "\t<div class=\"responder-info-parent\">\n";   
    foreach($value as $row2 => $value2) {
        echo "\t\t<div class=\"responder-info\">" . $value2 . "</div>\n";
}
echo "\t</div>\n";
}

这给我带来了:

代码语言:javascript
复制
<div class="responder-info-parent">
    <div class="responder-info">Firstname Lastname</div>
    <div class="responder-info">01234567896</div>
    <div class="responder-info">1 hours ago</div>
    <div class="responder-info">LR066</div>
    <div class="responder-info">amber</div>
</div><!-- close parent-->

我希望显示琥珀图标,而不是琥珀这个词。

例如:

代码语言:javascript
复制
<div class="responder-info-parent">
        <div class="responder-info">First Name</div>
        <div class="responder-info">123456789</div>
        <div class="responder-info">1 hours ago</div>
        <div class="responder-info">LR066</div>
        <div class="responder-info"><img src="/images/amber-icon.png"></div>
    </div><!-- close parent-->

所以。我的理解是,我需要测试一个特定的键(IconColour),如果匹配,则输出与其他键不同的html。

这就是我被卡住的地方。我知道我需要做什么,并且已经搜索了散列数组中的特定键,但还没有找到解决方案。尽管我知道,一旦我发布了这篇文章(并获得投票),我就会立即找到答案;-)

为了完整性。这是我的$results_array的var_dump

谢谢你的帮助。

代码语言:javascript
复制
array(7) {
  [0]=>
  array(5) {
    ["Name"]=>
    string(15) "First Name"
    ["telephone"]=>
    string(11) "123456789"
    ["updated"]=>
    string(11) "1 hours ago"
    ["LRNumber"]=>
    string(5) "LR066"
    ["IconColour"]=>
    string(5) "amber"
  }
  [1]=>
  array(5) {
    ["Name"]=>
    string(12) "First Name"
    ["telephone"]=>
    string(11) "01234568796"
    ["updated"]=>
    string(11) "4 hours ago"
    ["LRNumber"]=>
    string(5) "LR011"
    ["IconColour"]=>
    string(5) "amber"
  }
  [2]=>
  array(5) {
    ["Name"]=>
    string(19) "First Name"
    ["telephone"]=>
    string(11) "01234568796"
    ["updated"]=>
    string(10) "1 days ago"
    ["LRNumber"]=>
    string(5) "LR005"
    ["IconColour"]=>
    string(4) "grey"
  }
  [3]=>
  array(5) {
    ["Name"]=>
    string(8) "First Name"
    ["telephone"]=>
    string(5) "LR076"
    ["updated"]=>
    string(11) "80 days ago"
    ["LRNumber"]=>
    string(11) "01234568796"
    ["IconColour"]=>
    string(4) "grey"
  }
  [4]=>
  array(5) {
    ["Name"]=>
    string(14) "First Name"
    ["telephone"]=>
    string(11) "01234568796"
    ["updated"]=>
    string(11) "1 hours ago"
    ["LRNumber"]=>
    string(5) "LR036"
    ["IconColour"]=>
    string(5) "amber"
  }
  [5]=>
  array(5) {
    ["Name"]=>
    string(11) "First Name"
    ["telephone"]=>
    string(11) "01234568796"
    ["updated"]=>
    string(13) "0 minutes ago"
    ["LRNumber"]=>
    string(5) "LR002"
    ["IconColour"]=>
    string(5) "green"
  }
  [6]=>
  array(5) {
    ["Name"]=>
    string(13) "First Name"
    ["telephone"]=>
    string(11) "01234568796"
    ["updated"]=>
    string(11) "7 hours ago"
    ["LRNumber"]=>
    string(5) "LR003"
    ["IconColour"]=>
    string(5) "amber"
  }
}
EN

回答 2

Stack Overflow用户

发布于 2018-06-17 01:21:55

代码语言:javascript
复制
if ($row2 == 'IconColour') {
    echo "\t\t<div class=\"responder-info\"><img src=\"/images/" . $value2 . "-icon.png\"></div>\n";
} else {
    // whatever you have now
}
票数 1
EN

Stack Overflow用户

发布于 2018-06-17 01:25:08

foreach loop中,您有一个键和一个值。你可以检查当前的key ==是否为"IconColour“,并显示图片,而不只是回显该值。然后你可以回显颜色(值)和硬编码的图像名称的其他部分,如果你有所有的图标命名为:“颜色”-icon.png。示例:

代码语言:javascript
复制
$results_array [] = array("Name"=>'First Name', "telephone"=>'123456789', "updated"=>'1 hours ago', "LRNumber"=>'LR066', "IconColour"=>'amber');

foreach($results_array as $key => $value){
    echo "\t<div class=\"responder-info-parent\">\n";   
    foreach($value as $key2 => $value2) {
       if ($key2 == "IconColour") {
           echo "\t\t<div class=\"responder-info\"><img src=\"/images/" . $value2 . "-icon.png\"</div>\n"; 
       } else {
           echo "\t\t<div class=\"responder-info\">" . $value2 . "</div>\n";
       }

   }
   echo "\t</div>\n";
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50889971

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档