2011-02-08 63 views
0

我无法将数组转换为正确的嵌套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任何麻烦,它只是我不知道如何组织数据。在将数据拉回到一起之前,将它们分成三个不同的数组(数字,作者,书籍)?但是,我如何将这些书联系起来(Letter-> Author-> Book)?

感谢您的任何想法。

+0

首先你需要通过一些教程如何遍历一个数组php – 2011-02-08 05:27:57

+0

你可以print_r你的数组?你是否需要某人为你写一个函数来分割每一行,把它分类成字母,然后如图所示输出它? – Jason 2011-02-08 05:30:07

回答

2

假设您的阵列已经排序为例:

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> 
0

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

$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) { 

     } 

    } 

} 
0

你已经g那里没有div汤,但是你解析你的数组。

尝试使用列表输出一些有意义的和语义的东西。