dongnei3634 2017-10-23 04:04
浏览 41
已采纳

通过PHP关联数组键分隔HTML容器

I have a list of PHP associative arrays. Each associative array in the list is a color. It has a hex code, a label, and a family. The family will be basic colors: blue, red, green, etc., and the labels will be more specific colors, e.g. for the blue family there could be: aqua marine, teal, sky blue.

In my template, I iterate over the list of associative arrays, creating a basic two part container, the top half just being the hex color, and the bottom half describing it with its hex code, and label name. I want each item to be in one large container for whatever family it is. For example aqua marine, teal, and sky blue would go in one container with a header 'blue', and there might also be lime, forest, and yellow-green in a container with a header 'green'. I order the list of associative arrays by the family field ahead of time so what I really just need to find out is how to tell my html to create a new 'family container' when the value of family changes.

I'm a Python developer, and am new to all things PHP.

PHP HTML

<?php

$allColors = array(
  '0' => array(
  "id"=> "1",
  "family"=> "blue",
  "name"=> "ariel blue",
  "hex"=> "#339FFF"),

 '3' => array(
  "id"=> "3",
  "family"=> "green",
  "name"=> "forest",
  "hex"=> "#FAFF33"),

 '1' => array(
  "id"=> "2",
  "family"=> "blue",
  "name"=> "aqua marine",
  "hex"=> "#339FFF"),

 '4' => array(
  "id"=> "4",
  "family"=> "green",
  "name"=> "lime",
  "hex"=> "#FAFF33"),

 '2' => array(
  "id"=> "5",
  "family"=> "blue",
  "name"=> "teal",
  "hex"=> "#339FFF"),

 '5' => array(
  "id"=> "6",
  "family"=> "green",
  "name"=> "yellow-green",
  "hex"=> "#FAFF33")
); 

array_multisort( array_column( $allColors, 'family') , SORT_DESC, $allColors );

$key_list = ["blue", "green"]

?> 


<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>

    <link rel="stylesheet" type="text/css" href="main.css">
  </head>

 <body>

 <?php 
  $currFamily = $allColors[0]["family"]
 ?>

  <?php foreach($allColors as $key => $value): ?>

    if ($value["family"] != $currFamily) {
      <div id="family-container" style="border: 1px solid <?php echo $value["family"]; ?>; background-color: $currFamily;">
    }

      <h2>Blue <?php echo $currFamily; ?></h2>

       <div class="child-container" style="margin-right: 20px; width: 100px; height: 150px; border: 1px solid black; float: left;">
          <div style="width: 100%; height: 100px; float: right; border-bottom: 1px solid black; background: <?php echo $value["hex"]; ?>;"></div>
          <div style="width: 100%; height: 50px; float: right;">
            <?php echo $value["name"]; ?>;
            <?php echo $value["hex"]; ?>;
          </div>
        </div>

        if ($value["family"] != $currFamily) {
          </div>

          $currFamily = $value["family"]
        }

  <?php endforeach; ?>

 </body>
</html>

enter image description here

  • 写回答

1条回答 默认 最新

  • doushi5752 2017-10-23 04:29
    关注

    You can group your colours by family and you will have readable view like below

    <?php
    
    $allColors = []; 
    $arGroupedByFamily = [];
    foreach ($allColors as $color) {
        $arGroupedByFamily[$color['family']][] = $color;
    }
    
    ?> 
    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8">
            <title></title>
            <link rel="stylesheet" type="text/css" href="main.css">
        </head>
    
        <body>
            <?php foreach ($arGroupedByFamily as $key => $family): ?>
                <div id="family-container" style="border: 1px solid <?php echo $key; ?>; background-color: $currFamily;">
                    <?php foreach ($family as $color): ?>
                        <h2><?php echo $color['name'];?> <?php echo $key; ?></h2>
                        <div class="child-container" style="margin-right: 20px; width: 100px; height: 150px; border: 1px solid black; float: left;">
                            <div style="width: 100%; height: 100px; float: right; border-bottom: 1px solid black; background: <?php echo $color["hex"]; ?>;"></div>
                            <div style="width: 100%; height: 50px; float: right;">
                                <?php echo $color["name"]; ?>;
                                <?php echo $color["hex"]; ?>;
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            <?php endforeach; ?>
        </body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化