数组数据到嵌套 HTML
我在将数组转换为正确嵌套的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设您的数组已按示例排序:
示例数组的输出:
Assuming your array is already sorted as the example:
Output from your sample array:
您还可以生成一个多维数组,这可能需要更少的代码来呈现:
You can also produce a multidimensional array which may require less code to render:
你那里有 div soup,但是你解析你的数组。
尝试使用列表输出一些有意义且语义化的内容。
You've got div soup there, however you parse your arrays.
Try outputting something meaningful and semantic using lists.