数组数据到嵌套 HTML

发布于 2024-10-16 12:18:05 字数 1271 浏览 2 评论 0原文

我在将数组转换为正确嵌套的 HTML 时遇到问题。假设我有一个包含以下示例值的数组 -

An, Author---Some Book
Another, Author---A Book
Another, Author---Another Book
Be, Author---Book 1
Be, Author---Book 2
No, Author---Book 1

如何将其转换为有组织的 HTML -

<div>
    <div class="letter">A</div>
    <div>
        <div class="author">An, Author</div>
        <div class="title">Some Book</div>
    </div>
    <div>
        <div class="author">Another, Author</div>
        <div class="title">A Book</div>
        <div class="title">Another Book</div>
    </div>
<div>
<div>
    <div class="letter">B</div>
    <div>
        <div class="author">Be, Author</div>
        <div class="title">Book 1</div>
        <div class="title">Book 2</div>
    </div>
<div>
<div>
    <div class="letter">N</div>
    <div>
        <div class="author">No, Author</div>
        <div class="title">Book 1</div>
    </div>
<div>

我对 PHP 没有任何问题,只是我不确定如何组织数据。在将数据拉回到一起之前分成三个不同的数组(信件、作者、书籍)?但是我该如何关联这些书(信->作者->书)?

感谢您的任何想法。

I'm having trouble converting an array into correctly nested HTML. Assuming I have an array with the following example values -

An, Author---Some Book
Another, Author---A Book
Another, Author---Another Book
Be, Author---Book 1
Be, Author---Book 2
No, Author---Book 1

How do turn that into an organised HTML like -

<div>
    <div class="letter">A</div>
    <div>
        <div class="author">An, Author</div>
        <div class="title">Some Book</div>
    </div>
    <div>
        <div class="author">Another, Author</div>
        <div class="title">A Book</div>
        <div class="title">Another Book</div>
    </div>
<div>
<div>
    <div class="letter">B</div>
    <div>
        <div class="author">Be, Author</div>
        <div class="title">Book 1</div>
        <div class="title">Book 2</div>
    </div>
<div>
<div>
    <div class="letter">N</div>
    <div>
        <div class="author">No, Author</div>
        <div class="title">Book 1</div>
    </div>
<div>

I don't have any trouble with PHP, its just I'm not sure how I would organise the data. Split into three different arrays (Letters, Authors, Books) before pulling the data back together? But then how would I associate the books (Letter->Author->Book)?

Thanks for any ideas.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蘸点软妹酱 2024-10-23 12:18:05

假设您的数组已按示例排序:

for ($i = 0; $i < count($array); $i++) {
    $parts = explode("---", $array[$i]);
    $author = $parts[0];
    $book = $parts[1];
    $letter = substr($author, 0, 1);

    if (!isset($last_author) || $last_author != $author) {

        if ($i > 0 && $last_letter != $letter) {
                echo "\t</div>\n";
                echo "</div>\n";
        } else if ($i > 0) {
                echo "\t</div>\n";
        }

        if (!isset($last_letter) || $last_letter != $letter) {
            echo "<div>\n";
            echo "\t" . '<div class="letter">' . strtoupper($letter) . '</div>' . "\n";
            $last_letter = $letter;
        }

        echo "\t<div>\n";

        echo "\t\t" . '<div class="author">' . $author . '</div>' . "\n";
        echo "\t\t" . '<div class="title">' . $book . '</div>' . "\n";

        $last_author = $author;

    } else {
        echo "\t\t" . '<div class="title">' . $book . '</div>' . "\n";
    }

}

if (count($array) > 0) echo "\t</div>\n</div>";

示例数组的输出:

<div>
        <div class="letter">A</div>
        <div>
                <div class="author">An, Author</div>
                <div class="title">Some Book</div>
        </div>
        <div>
                <div class="author">Another, Author</div>
                <div class="title">A Book</div>
                <div class="title">Another Book</div>
        </div>
</div>
<div>
        <div class="letter">B</div>
        <div>
                <div class="author">Be, Author</div>
                <div class="title">Book 1</div>
                <div class="title">Book 2</div>
        </div>
</div>
<div>
        <div class="letter">N</div>
        <div>
                <div class="author">No, Author</div>
                <div class="title">Book 1</div>
        </div>
</div>

Assuming your array is already sorted as the example:

for ($i = 0; $i < count($array); $i++) {
    $parts = explode("---", $array[$i]);
    $author = $parts[0];
    $book = $parts[1];
    $letter = substr($author, 0, 1);

    if (!isset($last_author) || $last_author != $author) {

        if ($i > 0 && $last_letter != $letter) {
                echo "\t</div>\n";
                echo "</div>\n";
        } else if ($i > 0) {
                echo "\t</div>\n";
        }

        if (!isset($last_letter) || $last_letter != $letter) {
            echo "<div>\n";
            echo "\t" . '<div class="letter">' . strtoupper($letter) . '</div>' . "\n";
            $last_letter = $letter;
        }

        echo "\t<div>\n";

        echo "\t\t" . '<div class="author">' . $author . '</div>' . "\n";
        echo "\t\t" . '<div class="title">' . $book . '</div>' . "\n";

        $last_author = $author;

    } else {
        echo "\t\t" . '<div class="title">' . $book . '</div>' . "\n";
    }

}

if (count($array) > 0) echo "\t</div>\n</div>";

Output from your sample array:

<div>
        <div class="letter">A</div>
        <div>
                <div class="author">An, Author</div>
                <div class="title">Some Book</div>
        </div>
        <div>
                <div class="author">Another, Author</div>
                <div class="title">A Book</div>
                <div class="title">Another Book</div>
        </div>
</div>
<div>
        <div class="letter">B</div>
        <div>
                <div class="author">Be, Author</div>
                <div class="title">Book 1</div>
                <div class="title">Book 2</div>
        </div>
</div>
<div>
        <div class="letter">N</div>
        <div>
                <div class="author">No, Author</div>
                <div class="title">Book 1</div>
        </div>
</div>
铜锣湾横着走 2024-10-23 12:18:05

您还可以生成一个多维数组,这可能需要更少的代码来呈现:

$new = array();

for ($i = 0; $i < count($array); $i++) {
    $parts = explode("---", $array[$i]);
    $author = $parts[0];
    $book = $parts[1];
    $letter = substr($author, 0, 1);

    $new[$letter][$author][] = $book;
}

foreach ($new as $letter => $authors) {

    foreach ($authors as $author => $books) {

        foreach ($books as $book) {

        }

    }

}

You can also produce a multidimensional array which may require less code to render:

$new = array();

for ($i = 0; $i < count($array); $i++) {
    $parts = explode("---", $array[$i]);
    $author = $parts[0];
    $book = $parts[1];
    $letter = substr($author, 0, 1);

    $new[$letter][$author][] = $book;
}

foreach ($new as $letter => $authors) {

    foreach ($authors as $author => $books) {

        foreach ($books as $book) {

        }

    }

}
只是偏爱你 2024-10-23 12:18:05

你那里有 div soup,但是你解析你的数组。

尝试使用列表输出一些有意义且语义化的内容。

You've got div soup there, however you parse your arrays.

Try outputting something meaningful and semantic using lists.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文