duannian7116
duannian7116
2011-05-22 23:06

按作者的名称对数组中的项进行聚类

  • cluster-analysis
  • php
  • arrays
已采纳

I have an Array like this:

[1] "John", "content1"
[2] "John", "content2"
[3] "John", "content3"
[4] "Jim", "content"
[5] "Joana", "content1"
[6] "Joana", "content2"

I attempted to have an output like this:

<ul>

 <li>John
  <ul>
   <li> content 1 </li>
   <li> content 2 </li>
   <li> content 3 </li>
  </ul>
 </li>

 <li>Jim
  <ul>
   <li> content </li>
  </ul>
 </li>

 <li>Joana
  <ul>
   <li> content 1 </li>
   <li> content 2 </li>
  </ul>
 </li>

</ul>

I tried solving it using a foreach-iteration, but it doesn't work out at all: (note: $cur_name means the author's name whereas $content means the content)

 foreach($array as $a) { 
              $cur_name = $a['name'];
    if ($old_name != $cur_name && $cur_name !="")$return .= "<li>";
    if ($old_name != $cur_name && $cur_name !="") {
        $return .= "<strong>$cur_name</strong><br />";
    }
    elseif($cur_name == "") {
    }
    elseif ($old_name == $cur_name) {
    }
    $return .= "$content<br />";
    if ($old_name != $cur_name && $cur_name !="") $return .= "</li>";

    $old_name = $cur_name;

    }
}

Any ideas highliy appreciated..

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • doudi7782 doudi7782 10年前

    Use this code:

    Do something like this:

    • iterate your array and create another key-ed array (say $processedArray) where key value taken from 1st index on each array
    • on each iteration, add 2st index to it's appropriate 1st key.
    • after that, iterate this $processedArray to output it appropriately.

    I know reading code would be easier, so, try this one:

    <?php
    $data = array(
        array('name'=>'John', 'content'=>'Content1'),
        array('name'=>'John', 'content'=>'Content2'),
        array('name'=>'John', 'content'=>'Content3'),
        array('name'=>'Jim', 'content'=>'Content'),
        array('name'=>'Joana', 'content'=>'Content1'),
        array('name'=>'Joana', 'content'=>'Content2'),
    );
    
    $processedArray = array();
    
    foreach( $data as $each )
    {
        if( !isset( $processedArray[ $each[ 'name' ] ] ))
            $processedArray[ $each[ 'name' ] ] = array();
        $processedArray[ $each[ 'name' ] ][] = $each[ 'content' ];
    }
    
    echo "<ul>
    ";
    
    foreach( $processedArray as $key => $arrVal)
    {
        echo "  <li>$key
    ";
        echo "    <ul>
    ";
        foreach( $arrVal as $value )
        {
            echo "      <li>$value</li>
    ";
        }
        echo "    </ul>
    ";
        echo "  </li>
    ";
    }
    echo "</ul>
    ";
    ?>
    
    点赞 评论 复制链接分享
  • dpj775835868 dpj775835868 10年前

    Here is some basic code that will do it for you:

    <ul>
    <?php
    $array = array(
                    array(  name    => 'John',
                            content => 'content1'),
                    array(  name    => 'John',
                            content => 'content2'),
                    array(  name    => 'John',
                            content => 'content3'),
                    array(  name    => 'Jim',
                            content => 'content'),
                    array(  name    => 'Joana',
                            content => 'content1'),
                    array(  name    => 'Joana',
                            content => 'content2')
    );
    foreach($array as $a) {
        if ($a['name'] != $old_name) {
            if (isset($old_name)) $return .= "\t\t</ul>
    \t</li>
    ";
            $return .= "\t<li><strong>" . $a['name'] . "</strong>
    \t\t<ul>
    ";
        }
        $return .= "\t\t\t<li>" . $a['content'] . "</li>
    ";
        $old_name = $a['name'];
    }
    echo "$return\t\t</ul>
    \t</li>
    ";
    ?>
    </ul>
    
    点赞 评论 复制链接分享
  • doude1917 doude1917 10年前

    Judging by what you've posted, I assume the data looks like:

    $array = array();
    $array[1] = array("name" => "John", "content" => "content1");
    $array[2] = array("name" => "John", "content" => "content2");
    $array[3] = array("name" =>"John", "content" =>"content3");     
    $array[4] = array("name" =>"Jim", "content" =>"content");
    $array[5] = array("name" =>"Joana", "content" =>"content1");
    $array[6] = array("name" =>"Joana", "content" =>"content2");
    

    If that's the case, the only thing you're missing is that $return .= "$content<br />"; should look like $return.=$a['content']."<br/>". Also, your code has an extra curly brace at the end, but I assume that's just from copy/paste. And of course, you need to echo the $return variable.

    点赞 评论 复制链接分享

为你推荐