【问题标题】:PHP Generate dynamic HTML based on bidimensional array valuesPHP 根据二维数组值生成动态 HTML
【发布时间】:2019-01-12 10:23:15
【问题描述】:

我有以下$MessagesArray一维关联内部数组的PHP二维索引数组:

Array
(
    [0] => Array
        (
            [id] => 1
            [from] => Person 1
            [to] => Person 2
            [platform] => Instagram Direct Messaging
            [date] => 2016/11/27
            [time] => 11:00
            [ampm] => AM
            [specialcontent] => none
            [content] => Hello
        )

    [1] => Array
        (
            [id] => 2
            [from] => Person 1
            [to] => Person 2
            [platform] => Instagram Direct Messaging
            [date] => 2016/11/27
            [time] => 11:00
            [ampm] => AM
            [specialcontent] => none
            [content] => How are you?
        )

    [2] => Array
        (
            [id] => 3
            [from] => Person 2
            [to] => Person 1
            [platform] => Instagram Direct Messaging
            [date] => 2016/11/27
            [time] => 6:00
            [ampm] => PM
            [specialcontent] => none
            [content] => Oh, hey there. I'm fine
        )

    [3] => Array
        (
            [id] => 4
            [from] => Person 2
            [to] => Person 1
            [platform] => Instagram Direct Messaging
            [date] => 2016/11/27
            [time] => 6:01
            [ampm] => PM
            [specialcontent] => none
            [content] => What about you?
        )

    [4] => Array
        (
            [id] => 5
            [from] => Person 1
            [to] => Person 2
            [platform] => Instagram Direct Messaging
            [date] => 2016/11/28
            [time] => 8:00
            [ampm] => AM
            [specialcontent] => none
            [content] => It's been a while.
        )

    [5] => Array
        (
            [id] => 6
            [from] => Person 1
            [to] => Person 2
            [platform] => Instagram Direct Messaging
            [date] => 2016/11/30
            [time] => 2:00
            [ampm] => PM
            [specialcontent] => none
            [content] => Hello?
        )

    [6] => Array
        (
            [id] => 7
            [from] => Person 2
            [to] => Person 1
            [platform] => Instagram Direct Messaging
            [date] => 2016/12/01
            [time] => 3:00
            [ampm] => PM
            [specialcontent] => none
            [content] => Sorry, I'm traveling 'till next year.
        )

    [7] => Array
        (
            [id] => 8
            [from] => Person 1
            [to] => Person 2
            [platform] => Instagram Direct Messaging
            [date] => 2016/12/05
            [time] => 1:00
            [ampm] => PM
            [specialcontent] => none
            [content] => Fine by me.
        )

    [8] => Array
        (
            [id] => 9
            [from] => Person 2
            [to] => Person 1
            [platform] => Instagram Direct Messaging
            [date] => 2017/01/04
            [time] => 3:00
            [ampm] => PM
            [specialcontent] => none
            [content] => I'm back.
        )

    [9] => Array
        (
            [id] => 10
            [from] => Person 1
            [to] => Person 2
            [platform] => Instagram Direct Messaging
            [date] => 2018/01/15
            [time] => 1:00
            [ampm] => PM
            [specialcontent] => none
            [content] => I'm back too, one year later too.
        )

)

谁的实际代码是(尽管我实际上是从 XML 文件中的数据库创建了前一个数组):

<?php
    $MessagesArray = array(
        array(
            "id" => "1",
            "from" => "Person 1",
            "to" => "Person 2",
            "platform" => "Instagram Direct Messaging",
            "date" => "2016/11/27",
            "time" => "12:00",
            "ampm" => "AM",
            "specialcontent" => "none",
            "content" => "Hello"
        ),
        array(
            "id" => "2",
            "from" => "Person 1",
            "to" => "Person 2",
            "platform" => "Instagram Direct Messaging",
            "date" => "2016/11/27",
            "time" => "11:00",
            "ampm" => "AM",
            "specialcontent" => "none",
            "content" => "How are you?"
        ),
        array(
            "id" => "3",
            "from" => "Person 2",
            "to" => "Person 1",
            "platform" => "Instagram Direct Messaging",
            "date" => "2016/11/27",
            "time" => "6:00",
            "ampm" => "PM",
            "specialcontent" => "none",
            "content" => "Oh, hey there. I'm fine"
        ),
        array(
            "id" => "4",
            "from" => "Person 2",
            "to" => "Person 1",
            "platform" => "Instagram Direct Messaging",
            "date" => "2016/11/27",
            "time" => "6:01",
            "ampm" => "PM",
            "specialcontent" => "none",
            "content" => "What about you?"
        ),
        array(
            "id" => "5",
            "from" => "Person 1",
            "to" => "Person 2",
            "platform" => "Instagram Direct Messaging",
            "date" => "2016/11/28",
            "time" => "8:00",
            "ampm" => "AM",
            "specialcontent" => "none",
            "content" => "It's been a while."
        ),
        array(
            "id" => "6",
            "from" => "Person 1",
            "to" => "Person 2",
            "platform" => "Instagram Direct Messaging",
            "date" => "2016/11/30",
            "time" => "2:00",
            "ampm" => "PM",
            "specialcontent" => "none",
            "content" => "Hello?"
        ),
        array(
            "id" => "7",
            "from" => "Person 2",
            "to" => "Person 1",
            "platform" => "Instagram Direct Messaging",
            "date" => "2016/12/01",
            "time" => "3:00",
            "ampm" => "PM",
            "specialcontent" => "none",
            "content" => "Sorry, I'm traveling 'till next year."
        ),
        array(
            "id" => "8",
            "from" => "Person 1",
            "to" => "Person 2",
            "platform" => "Instagram Direct Messaging",
            "date" => "2016/12/05",
            "time" => "1:00",
            "ampm" => "PM",
            "specialcontent" => "none",
            "content" => "Fine by me."
        ),
        array(
            "id" => "9",
            "from" => "Person 2",
            "to" => "Person 1",
            "platform" => "Instagram Direct Messaging",
            "date" => "2017/01/04",
            "time" => "3:00",
            "ampm" => "PM",
            "specialcontent" => "none",
            "content" => "I'm back."
        ),
        array(
            "id" => "10",
            "from" => "Person 1",
            "to" => "Person 2",
            "platform" => "Instagram Direct Messaging",
            "date" => "2018/01/15",
            "time" => "1:00",
            "ampm" => "PM",
            "specialcontent" => "none",
            "content" => "I'm back too, one year later too."
        )
    );
?>

我想以某种方式有一个自动创建 HTML 的代码,例如:

<div class="year">
  2016
  <div class="month">
    Month 11
    <div class="day">
      Day 27
    </div>
    <div class="day">
      Day 28
    </div>
    <div class="day">
      Day 30
    </div>
  <div class="month">
    Month 12
    <div class="day">
      Day 01
    </div>
    <div class="day">
      Day 05
    </div>
  </div>
  </div>
</div>
<div class="year">
  2017
  <div class="month">
    Month 01
    <div class="day">
      Day 04
    </div>
  </div>
</div>
<div class="year">
  2018
  <div class="month">
    Month 01
    <div class="day">
      Day 15
    </div>
  </div>
</div>

如您所见,这并不是真的那么难,我只是在弄清楚如何使用嵌套的foreach 函数时遇到了问题。

代码应为每个唯一的年份生成一个&lt;div&gt; HTML 元素,然后为该年的每个唯一月份生成其对应的&lt;div&gt; HTML 元素,然后同时为每一天生成其对应的&lt;div&gt; HTML 元素当年那个月的那个月。

我认为我们需要使用嵌套的foreach 函数。假设我们有代码foreach ($MessagesArray as $item) {},那么在函数内部,我们可以分别得到$year = substr($item['date'], 0, 4);$month = substr($item['date'], 5, 2);$day = substr($item['date'], 8, 2);的年、月、日。我还认为我们需要有人知道上一次迭代的价值。

【问题讨论】:

  • 你可以为你的阵列做一个var_export() 吗?会让你更容易复制/帮助你。
  • 此外,您应该能够执行this post 之类的操作来将您的数组转换为对您更有意义的东西。然后遍历你的数组。
  • @ggorlen 我刚刚将它添加到我的帖子中!
  • @FrankerZ 你的意思是将数组的实际代码添加到我的帖子中吗?如果是这样,我只是这样做了 // 对于您链接的帖子,您的意思是我按其 ID 对数组进行排序?

标签: php html multidimensional-array


【解决方案1】:

是的,您需要编写能够识别前一行数据的代码。这可以通过一个循环和一些逻辑定位的条件语句来完成。

代码:(Demo)

$MessagesArray = [
    ["date" => "2016/11/27"],
    ["date" => "2016/11/27"],
    ["date" => "2016/11/27"],
    ["date" => "2016/11/27"],
    ["date" => "2016/11/28"],
    ["date" => "2016/11/30"],
    ["date" => "2016/12/01"],
    ["date" => "2016/12/05"],
    ["date" => "2017/01/04"],
    ["date" => "2018/01/15"]
];
$yr = null;
$mo = null;
$dy = null;

foreach ($MessagesArray as $row) {
    if ($row["date"] == "$yr/$mo/$dy") {
        continue;  // if repeat of previous date, move to next iteration
    }
    [$y, $m, $d] = explode("/", $row["date"]);
    if (($m != $mo || $y != $yr) && $mo !== null) {
        echo "</div>";
    }
    if ($y != $yr) {
        if ($yr !== null) {
            echo "</div>";
        }
        echo "<class='year'>$y";
    }
    if ($m != $mo || $y != $yr) {
        echo "<div class='month'>Month $m";
    }
    echo "<div class='day'>Day $d</div>";
    $yr = $y;
    $mo = $m;
    $dy = $d;
}
if (sizeof($MessagesArray)) {
    echo "</div></div>";
}

输出:

<div class='year'>2016
    <div class='month'>Month 11
        <div class='day'>Day 27</div>
        <div class='day'>Day 28</div>
        <div class='day'>Day 30</div>
    </div>
    <div class='month'>Month 12
        <div class='day'>Day 01</div>
        <div class='day'>Day 05</div>
    </div>
</div>
<div class='year'>2017
    <div class='month'>Month 01
        <div class='day'>Day 04</div>
    </div>
</div>
<div class='year'>2018
    <div class='month'>Month 01
        <div class='day'>Day 15</div>
    </div>
</div>

【讨论】:

  • 这个也是最准确的。我真的不知道该选择哪一个。 ?
  • 哈哈哈,也许我再问一个问题,你回答呢?它是对这个的补充,只是它包括回显消息。
  • 我将重新检查所有三个答案,并检查哪一个是最好的。
  • 老兄! 有一个问题,您的代码重复同年同月的几天,这是我不想要的。不过我想我可以自己解决这个问题。
  • 这就像检查日和月/年一样简单。
【解决方案2】:

这是一个幼稚的方法:

$messages = [
    ["date" => "2016/11/27"],
    ["date" => "2016/11/27"],
    ["date" => "2016/11/27"],
    ["date" => "2016/11/27"],
    ["date" => "2016/11/28"],
    ["date" => "2016/11/30"],
    ["date" => "2016/12/01"],
    ["date" => "2016/12/05"],
    ["date" => "2017/01/04"],
    ["date" => "2018/01/15"]
];

$parsed = [];

foreach ($messages as $k => $v) {
    [$y, $m, $d] = explode("/", $v["date"]);
    $parsed[$y][$m][$d] = 1;
}

$html = [];

foreach ($parsed as $y => $v) {
    $html[] = "<div class='year'>$y";
    
    foreach ($v as $m => $v) {
        $html[] = "<div class='month'>Month $m";

        foreach ($v as $d => $v) {
            $html[] = "<div class='day'>Day $d</div>";
        }
      
        $html[] = "</div>";
    }

    $html[] = "</div>";
}

echo implode($html);

输出:

<div class="year">2016<div class="month">Month 11<div class="day">Day 27</div><div class="day">Day 28</div><div class="day">Day 30</div></div><div class="month">Month 12<div class="day">Day 01</div><div class="day">Day 05</div></div></div><div class="year">2017<div class="month">Month 01<div class="day">Day 04</div></div></div><div class="year">2018<div class="month">Month 01<div class="day">Day 15</div></div></div>

说明:

在一个循环中解析结构,然后再次循环以生成 HTML。请注意,我简化了您的数据结构,但只要“日期”键存在,它就可以在任何其他数据结构上工作。此外,HTML 已缩小但完全相同——如果您想要一个漂亮的版本,那也不是什么大问题。

【讨论】:

  • 为什么要创建$html 数组?为什么不简单地把它呼出来?
  • 你为什么要回应它? OP 要求创建 HTML,而不是回显它。除非特别要求,否则我宁愿不编写带有副作用的代码——将其保持为数组或字符串格式为 OP 提供了下一步操作的最多选择,例如将其与一些 JSON 或 XML 一起发送到前端。
【解决方案3】:

重新格式化你的数组:

$formatted = [];

foreach ($MessagesArray as $Message) {
    $year = substr($Message['date'], 0, 4);
    $month = substr($Message['date'], 5, 2);
    $formatted[ $year ][ $month ][] = $Message;
}

print_r($formatted);

那么就是简单的循环遍历:

foreach ($formatted as $year => $months) {
    //Echo year text
    foreach ($months as $month => $messages) {
        //Echo month text
        foreach ($messages as $message) {
            //Echo each text
        }
    }
}

this php playground

【讨论】:

    猜你喜欢
    • 2011-01-14
    • 2012-05-21
    • 1970-01-01
    • 1970-01-01
    • 2015-10-15
    • 2013-02-16
    • 1970-01-01
    • 1970-01-01
    • 2019-10-17
    相关资源
    最近更新 更多