Clustering Items in an Array by Authors' names
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>
<开发者_Python百科;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..
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>\n";
foreach( $processedArray as $key => $arrVal)
{
echo " <li>$key\n";
echo " <ul>\n";
foreach( $arrVal as $value )
{
echo " <li>$value</li>\n";
}
echo " </ul>\n";
echo " </li>\n";
}
echo "</ul>\n";
?>
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.
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>\n\t</li>\n";
$return .= "\t<li><strong>" . $a['name'] . "</strong>\n\t\t<ul>\n";
}
$return .= "\t\t\t<li>" . $a['content'] . "</li>\n";
$old_name = $a['name'];
}
echo "$return\t\t</ul>\n\t</li>\n";
?>
</ul>
精彩评论