微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php中将多维数组输出为3列div和ul li

如何解决php中将多维数组输出为3列div和ul li

这是一个数组:我试图将数组拆分为 3 列 div,第一个数组应包装到 h3 标记中,然后其他数组应显示在无序列表中。

Array
(
    [0] => Array
        (
            [label_01] => heading 01:
            [label_02] => heading 02: 
            [label_03] => heading 03: 
        )

    [1] => Array
        (
            [label_01] => item 01
            [label_02] => item 02
            [label_03] => item 03
        )

    [2] => Array
        (
            [label_01] => item 001
            [label_02] => item 002
            [label_03] => item 003
        )

)

它应该像这样输出

<div class="row">
    <div class="col-lg-4">
        <h3 class="text-primary text-center">heading 01:</h3>
        <ul class="list-Box">
            <li class="bg-primary text-white">item 01</li>
            <li class="bg-primary text-white">item 001</li>
        </ul>
    </div>
    <div class="col-lg-4">
        <h3 class="text-warning text-center">heading 02:</h3>
        <ul class="list-Box">
            <li class="bg-warning text-white">item 02</li>
            <li class="bg-warning text-white">item 002</li>
        </ul>
    </div>
    <div class="col-lg-4">
        <h3 class="text-blue text-center">heading 03:</h3>
        <ul class="list-Box">
            <li class="bg-blue text-white">item 03</li>
            <li class="bg-blue text-white">item 003</li>
        </ul>
    </div>
</div>

到目前为止我已经尝试过

<div class="row">
    <?PHP 
    $keys = array_keys($columns);
    for($i = 0; $i < count($columns); $i++) { ?>
        <div class="col-lg-4">
    <?PHP foreach($columns[$keys[$i]] as $key => $value) { ?>
            <li class="bg-primary text-white"><?PHP echo $value; ?></li>
    <?PHP } ?>
        </div>
    <?PHP } ?>
</div>
                </div>
            <?PHP } ?>
            </div>

还有这个

<div class="row">
    <?PHP foreach ( $columns as $column ) { ?>
    <div class="col-lg-4">
            <h3 class="text-primary text-center">Inspire: Self leadership</h3>
            <ul class="list-Box">
        <?PHP foreach ($column as $val) { ?>
        <li class="bg-primary text-white"><?PHP echo $val; ?></li>
        
        <?PHP } ?>
        </ul>
    </div>
    <?PHP } ?>
</div>

任何帮助将不胜感激!提前致谢!

解决方法

首先你的阵列不适合你想做的事情。所以剩下2个选项;在第一个场景中,您必须首先更改数组结构(通过更改数组的键值使循环数组更适合您的需要),第二个场景是一个小的静态解决方案,但也许它可以解决您的问题,现在是;

<?php 
$your_array = array(
    0 => array(
        'label_01' => 'heading 01:','label_02' => 'heading 02:','label_03' => 'heading 03:',),1 => array(
        'label_01' => 'item 01','label_02' => 'item 02','label_03' => 'item 03',2 => array(
        'label_01' => 'item 001','label_02' => 'item 002','label_03' => 'item 003',); //this is your sample array :P  

?>
<div class="row">
<?php 
$headers = $your_array[0]; // fetch the headers first... because our main loop is headers in your case...
$items = $your_array[1]; // fetch the items... we do it because your array is unordered for your needs...
$sub_items = $your_array[2]; // fetch the sub_items... we do it because your array is unordered for your needs...

foreach($headers as $key => $val) {
?>
<div class="col-lg-4">
    <h3 class="text-primary text-center"><?php echo $val; ?></h3>
    <ul class="list-box">
        <?php 
        foreach($items as $key2 => $val2) { 
            if($key2 == $key) {
        ?>
        <li class="bg-primary text-white"><?php echo $val2; ?></li>
        <?php 
            }
        } 

        foreach($sub_items as $key2 => $val2) { 
            if($key2 == $key) {
        ?>
        <li class="bg-primary text-white"><?php echo $val2; ?></li>
        <?php 
            }
        } 
        ?>
    </ul>
</div>
<?php } ?>
</div>
,

使用该数组的副本和 PHP heredoc 插值的强大功能,您可以执行一些 while 循环。复制是为了保持原始数组的完整性。如果你不介意,可以直接使用$data

$data = [
    [
        'label_01' => 'heading 01:','label_03' => 'heading 03:'
    ],[
        'label_01' => 'item 01','label_03' => 'item 03'
    ],[
        'label_01' => 'item 001','label_03' => 'item 003'
    ]
];

echo '<div class="row">',PHP_EOL;
$copy = $data;
while(count($copy[0]))
{
    $h3  = array_shift($copy[0]);
    $li1 = array_shift($copy[1]);
    $li2 = array_shift($copy[2]);
    echo <<<"_HTML"
    <div class="col-lg-4">
        <h3 class="text-primary text-center">$h3</h3>
        <ul class="list-box">
            <li class="bg-primary text-white">$li1</li>
            <li class="bg-primary text-white">$li2</li>
        </ul>
    </div>
_HTML;
}
echo '</div>',PHP_EOL;

为了使它如评论中所要求的那样灵活,这是另一种方法。假设总是有一个标题和灵活的 li 数量。

$copy = $data;
while(count($copy[0]))
{
    $rowdata = [];
    foreach($copy as &$value) {
        $rowdata[] = array_shift($value);
    }
    unset($value);
    $headline = array_shift($rowdata);
    $li = '';
    foreach($rowdata as $value) {
        $li .= "<li class=\"bg-primary text-white\">$value</li>\n";
    }

    echo <<<"_HTML"
    <div class="col-lg-4">
        <h3 class="text-primary text-center">$headline</h3>
        <ul class="list-box">
            $li
        </ul>
    </div>
_HTML;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。