ultrus Posted March 30, 2009 Share Posted March 30, 2009 Hello, I have an interesting puzzle. I have a list of places, with places that have parent places assigned to them in the same list. Example: North America and Colorado are in the same list, and North America is the parent of Colorado. I have another list of locations/areas that are assigned to places in the places list. Example: "Coverage 3" is assigned to Colorado. I can easily run a function to tally up the locations in each place (see below), but that's the first step. The second step(s) is to get a total tally of each parent place including all the tallys of the children places. Ouch!@! Example: North Colorado has 2 total, Colorado has 1 total, and North America has 1 total to start. The Total of North America with children would be 4. Here's what I have so far: <?php $places = array( array("id"=>1, "name"=>"North America", "count"=>0, "parentID"=>0), array("id"=>2, "name"=>"Colorado", "count"=>0, "parentID"=>1), array("id"=>3, "name"=>"North Colorado", "count"=>0, "parentID"=>2) ); $coverageAreas = array( array("placeID"=>3, "name"=>"Coverage 1"), array("placeID"=>3, "name"=>"Coverage 2"), array("placeID"=>2, "name"=>"Coverage 3"), array("placeID"=>1, "name"=>"Coverage 4"), ); function getPlaceCountTotals() { global $places, $coverageAreas; //get direct place counts for($i=0; $i<count($coverageAreas); $i++) { $for($p=0; $p<count($places); $p++) { if($coverageAreas[$i]["placeID"] == $places[$p]["id"]) { $places[$p]["count"]++; break; } } } //?? ok, now how do I add children counts to parent counts?? //Example: //Before: //North America = 1 //Colorado = 1 //North Colorado = 2 //After: //North America = 4 //Colorado = 3 //North Colorado = 2 } getPlaceCountTotals(); ?> The above example is way simplified. In reality places are all over the place not in any particular order (at first anyway). Any thoughts on how to proceed? I don't need a full example, but any pointers to help get me over the hump would be greatly appreciated! Thanks much in advance for the assist. Edit: Maybe if I can figure out the depth of each child, that would help... Link to comment https://forums.phpfreaks.com/topic/151808-interesting-array-puzzle-recursive-children-tallys/ Share on other sites More sharing options...
ultrus Posted March 30, 2009 Author Share Posted March 30, 2009 Ah I think I have it. Sometimes I just need to write things out and it makes more sense. I'll post something if it is worth while. Thank you Link to comment https://forums.phpfreaks.com/topic/151808-interesting-array-puzzle-recursive-children-tallys/#findComment-797119 Share on other sites More sharing options...
laffin Posted March 30, 2009 Share Posted March 30, 2009 I used something similar (w/o counting), to provide subcategories of unlimited depth to a download area. This way things could be very organized and looking them up would be simpler. I used Thr Tree Based structure which u have. than i built a cached index, so i wudnt have to keep rebuilding the tree. since the counting is very important, ya may consider keeping a counter updated so ya dun have to build the tree in order to get a count as this will take the most amount of time. Trees and Recursion, its a lot to take in, and at times may cause headaches, but it can be done. Link to comment https://forums.phpfreaks.com/topic/151808-interesting-array-puzzle-recursive-children-tallys/#findComment-797283 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.