Forum Moderators: coopster

Message Too Old, No Replies

Pulling grouped values out of an array

using PHP 4

         

AnonyMouse

12:59 pm on Apr 28, 2006 (gmt 0)

10+ Year Member



I have an array of 3 variables: category_id, category_name, group_id.

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?

eelixduppy

1:21 pm on Apr 28, 2006 (gmt 0)



Hello...

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 "&nbsp;&nbsp;INFO: $other<br>";
}
echo "<br><br>";
}

?>

Hope this helps!

eelix

patriko

2:59 am on Apr 29, 2006 (gmt 0)

10+ Year Member



The code above could work, but the way I read it he wants to be able to have multiple categories per group. This is how I would go about it:

<?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>");

?>

AnonyMouse

9:46 am on Apr 29, 2006 (gmt 0)

10+ Year Member



Thank you both for your comments - I'll have a crack at it later on today. Right now the sun is shining and I need coffee!

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.)

patriko

12:53 pm on Apr 29, 2006 (gmt 0)

10+ Year Member



Ah. Well what you could do is loop through and create an array based on the group_id (like the one I started with above). Here's how I'd go about it (using the $info array that you gave in your previous message):


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.

AnonyMouse

7:44 pm on May 1, 2006 (gmt 0)

10+ Year Member



Got it! Thanks so much for your help, you pretty much had it nailed - certainly your code gave me enough insight to be able to reach the final solution:

//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!