PHP基于公共值在foreach中分离数组元素

时间:2014-03-19 00:49:02

标签: php arrays loops for-loop

我有来自数据库的以下数组:

$tags = Array
    (
        [0] => Array
            (
                [id] => 1
                [tag_name] => Testing/EOC
                [description] => 
                [category] => Category 1
            )

        [1] => Array
            (
                [id] => 2
                [tag_name] => Technology Tips
                [description] => 
                [category] => Category 1
            )

        [2] => Array
            (
                [id] => 3
                [tag_name] => Student Engagement
                [description] => 
                [category] => Category 1
            )

        [3] => Array
            (
                [id] => 4
                [tag_name] => Flipped Classroom
                [description] => 
                [category] => Category 1
            )

        [4] => Array
            (
                [id] => 5
                [tag_name] => Blended Instruction
                [description] => 
                [category] => Category 1
            )

        [5] => Array
            (
                [id] => 6
                [tag_name] => Differentiated Instruction
                [description] => 
                [category] => Category 1
            )

        [6] => Array
            (
                [id] => 7
                [tag_name] => Bootcamp
                [description] => 
                [category] => Category 1
            )

        [7] => Array
            (
                [id] => 8
                [tag_name] => Mathematical Practices 1
                [description] => 
                [category] => Category 1
            )

        [8] => Array
            (
                [id] => 9
                [tag_name] => Mathematical Practices 2
                [description] => 
                [category] => Category 2
            )

        [9] => Array
            (
                [id] => 10
                [tag_name] => Mathematical Practices 3
                [description] => 
                [category] => Category 2
            )

        [10] => Array
            (
                [id] => 11
                [tag_name] => Mathematical Practices 4
                [description] => 
                [category] => Category 2
            )

        [11] => Array
            (
                [id] => 12
                [tag_name] => Mathematical Practices 5
                [description] => 
                [category] => Category 2
            )

        [12] => Array
            (
                [id] => 13
                [tag_name] => Mathematical Practices 6
                [description] => 
                [category] => Category 2
            )

        [13] => Array
            (
                [id] => 14
                [tag_name] => Mathematical Practices 7
                [description] => 
                [category] => Category 2
            )

        [14] => Array
            (
                [id] => 15
                [tag_name] => Mathematical Practices 8
                [description] => 
                [category] => Category 2
            )

        [15] => Array
            (
                [id] => 16
                [tag_name] => Pre-Algebra
                [description] => 
                [category] => Category 3
            )

        [16] => Array
            (
                [id] => 17
                [tag_name] => Sets and Venn Diagrams
                [description] => 
                [category] => Category 3
            )
    )

现在我只是像这样循环:

<ul>
                    <? foreach($tags as $tag): ?>
                        <li>
                            <input type="checkbox" class="tag" name="tags[]" value="<?= $tag['id']; ?>">
                            <label><?= $tag['tag_name']; ?></label>
                        </li>
                    <? endforeach; ?>
</ul>

然而,我真正想做的是分离标签并根据三个不同的类别将它们列在三个部分(可能是它们自己的独立div)中。基本上我想要有三列,每列都有一个类别名称作为标题,下面列出了正确的标签。不确定如何在循环中完成此操作并将它们全部保存为相同形式的输入。

2 个答案:

答案 0 :(得分:0)

1按类别分组:

$categoryToTag = array();
foreach ($tags as $tag) {
    if (!isset($categoryToTag[$tag['category']])) $categoryToTag[$tag['category']] = array();
    $categoryToTag[$tag['category']][] = $tag;
}

2循环分类并输出标签:

foreach ($categoryToTag as $category => $categoryTags) {
    echo 'Category '.$category.' begin';
    foreach ($categoryTags as $tag) echo $tag;
}

答案 1 :(得分:0)

您可以考虑使用表格:

<table>
<?php
// group the sub-arrays of your initial array by category
$cats = array();
foreach($tags as $tag){
$cats[$tag["category"]][] = array($tag["id"], $tag["tag_name"]);
}
// display the names of the categories in the header row of the table
print("<tr><th>".implode("</th><th>", array_keys($cats))."</th></tr>");
// create another row
print("<tr>");
// iterate over the categories – display each category in a single table cell
foreach($cats as $cat){
print("<td>");
// iterate over the sub-arrays of each category
    foreach($cat as $val){
    print("<input type=\"checkbox\" class=\"tag\" name=\"tags[]\" value=\"{$val[0]}\">
                            <label>{$val[1]}</label><br/>");
    }
print("</td>");
}
print("</tr>");
?>
</table>