Forum Moderators: coopster
What I want to do is loop through the array, printing out the values, but in a clever way: I want to group them together using the group_id.
In other words, for each unique group_id, I want to print out all the categories that are in that group.
I've hunted for an array function that does the job, but can't find one.
Any suggestions?
If i understand correctly, you are looking for something like this:
<?php$info = array(
1=>array(
"category_id1",
"category_name1"
),
2=>array(
"category_id2",
"category_name2"
),
3=>array(
"category_id3",
"category_name3"
)
);foreach($info as $id=>$other)
{
echo "<b>ID: $id</b><br>";
foreach($other as $other)
{
echo " INFO: $other<br>";
}
echo "<br><br>";
}?>
Hope this helps!
eelix
<?php
// Load all the categories into their groups
$info = array (
1 => array (
array("group1_cat1_id", "group1_cat1_name"),
array("group1_cat2_id", "group1_cat2_name")
),
2 => array (
array("group2_cat1_id", "group2_cat1_name"),
array("group2_cat2_id", "group2_cat2_name"),
array("group2_cat3_id", "group2_cat3_name")
)
);
// Print out all the categories by group:
print ("<ul>");
while (list($group_id, $categories)=each($info)) {
print ("<li> Group #".$group_id);
print ("<ul>");
while (list($cat_id, $cat_name)=each($categories)) {
print ("<li> ".$cat_id.": ".$cat_name."<br>");
}
print ("</ul>");
}
print ("</ul>");
?>
Although having taken a closer look, I should have made it clear that the data is already in an array (courtesy of a database query) that looks like this:
$info = array(
1=>array(
"category_id1",
"category_name1",
"group_id1"
),
2=>array(
"category_id2",
"category_name2"
"group_id2"
),
3=>array(
"category_id3",
"category_name3"
"group_id3"
)
);
I.e. the array has 3 entities, not 2. What I want to do is loop through that array, and for each distinct groupid pull out all associated categories. (Obviously the array is bigger than the above, the category ids are unique, and they belong to one of a set of groupids.)
while (list($key,$obj)=each($info)) {
$sortedInfo[$obj[2]][sizeof($sortedInfo[$obj[2]])] = array($obj[0],$obj[1]);
}
This is assuming that in your array, group_id will always be the third element in each array (and it will be unique ... no repeats!). This should set up $sortedInfo in the way that I set up $info in my previous post.
//This bit loops through the existing database-query-generated array to create a multi-dimensional array $sortedInfo.
while (list($key,$obj)=each($catlist)) {
$sortedInfo[$obj['group_name']][sizeof($sortedInfo[$obj['group_name']])] = array($obj['cat_id'],$obj['cat_name']);
}
//This bit loops through the multidimensional array and prints out all the categroies per category group.
print ("<ul>");
while (list($group_name, $categories) = each($sortedInfo)) {
print ("<li> Group ".$group_name);
print ("<ul>");
while (list($a, $category) = each($categories)) {
print ("<li> ".$category[1]."<br>");
}
print ("</ul>");
}
print ("</ul>");
Can't emphasize how much you helped me, no way I could have cracked that without your pointers, thanks!