drisate Posted June 14, 2010 Share Posted June 14, 2010 Array ( [42] => Array ( [titre1] => Gestions des pages [parent] => 11 [idd] => 42 [add] => 0 [mod] => 0 [del] => 0 [adds] => 0 [mods] => 0 [dels] => 0 [dtype] => 11 ) [11] => Array ( [titre1] => Zone de gestion [parent] => 0 [idd] => 11 [add] => 3 [mod] => 0 [del] => 0 [adds] => 0 [mods] => 3 [dels] => 3 [dtype] => 14 ) [14] => Array ( [titre1] => HÉBERGEMENT [parent] => 0 [idd] => 14 [add] => 0 [mod] => 3 [del] => 0 [adds] => 0 [mods] => 0 [dels] => 0 [dtype] => 15 ) [34] => Array ( [titre1] => François Blaquière [parent] => 17 [idd] => 34 [add] => 0 [mod] => 3 [del] => 3 [adds] => 0 [mods] => 0 [dels] => 0 [dtype] => 11 ) [35] => Array ( [titre1] => Joëlle Bergeron [parent] => 17 [idd] => 35 [add] => 0 [mod] => 3 [del] => 3 [adds] => 0 [mods] => 0 [dels] => 0 [dtype] => 11 ) [36] => Array ( [titre1] => Patrick Simard [parent] => 17 [idd] => 36 [add] => 0 [mod] => 3 [del] => 3 [adds] => 0 [mods] => 0 [dels] => 0 [dtype] => 11 ) [37] => Array ( [titre1] => Florian Siretas [parent] => 17 [idd] => 37 [add] => 0 [mod] => 3 [del] => 3 [adds] => 0 [mods] => 0 [dels] => 0 [dtype] => 11 ) [38] => Array ( [titre1] => Valeria Valdes [parent] => 17 [idd] => 38 [add] => 0 [mod] => 3 [del] => 3 [adds] => 0 [mods] => 0 [dels] => 0 [dtype] => 11 ) [39] => Array ( [titre1] => Marie-Ève Matte [parent] => 17 [idd] => 39 [add] => 0 [mod] => 3 [del] => 3 [adds] => 0 [mods] => 0 [dels] => 0 [dtype] => 11 ) [15] => Array ( [titre1] => Sauvegarde [parent] => 0 [idd] => 15 [add] => 0 [mod] => 3 [del] => 0 [adds] => 0 [mods] => 0 [dels] => 0 [dtype] => 11 ) [16] => Array ( [titre1] => Entreprise [parent] => 0 [idd] => 16 [add] => 0 [mod] => 3 [del] => 0 [adds] => 0 [mods] => 0 [dels] => 0 [dtype] => 11 ) [17] => Array ( [titre1] => Équipe [parent] => 0 [idd] => 17 [add] => 3 [mod] => 0 [del] => 0 [adds] => 0 [mods] => 3 [dels] => 3 [dtype] => 14 ) [18] => Array ( [titre1] => Services [parent] => 0 [idd] => 18 [add] => 0 [mod] => 0 [del] => 0 [adds] => 0 [mods] => 0 [dels] => 0 [dtype] => 11 ) [19] => Array ( [titre1] => Centre de données [parent] => 0 [idd] => 19 [add] => 0 [mod] => 0 [del] => 0 [adds] => 0 [mods] => 0 [dels] => 0 [dtype] => 11 ) [20] => Array ( [titre1] => Aide aux utilisateurs [parent] => 0 [idd] => 20 [add] => 3 [mod] => 3 [del] => 0 [adds] => 3 [mods] => 3 [dels] => 3 [dtype] => 17 ) [21] => Array ( [titre1] => La configuration de votre courriel. [parent] => 20 [idd] => 21 [add] => 3 [mod] => 3 [del] => 0 [adds] => 3 [mods] => 3 [dels] => 3 [dtype] => 18 ) [23] => Array ( [titre1] => Configuration de Outlook Express [parent] => 23 [idd] => 23 [add] => 0 [mod] => 0 [del] => 0 [adds] => 0 [mods] => 0 [dels] => 0 [dtype] => 0 ) [24] => Array ( [titre1] => Configuration de Outlook 2003 [parent] => 21 [idd] => 24 [add] => 3 [mod] => 3 [del] => 3 [adds] => 0 [mods] => 0 [dels] => 0 [dtype] => 11 ) [25] => Array ( [titre1] => Configuration de Outlook 2007 [parent] => 21 [idd] => 25 [add] => 3 [mod] => 3 [del] => 3 [adds] => 0 [mods] => 0 [dels] => 0 [dtype] => 11 ) [40] => Array ( [titre1] => Site Internet [parent] => 26 [idd] => 40 [add] => 0 [mod] => 3 [del] => 3 [adds] => 0 [mods] => 0 [dels] => 0 [dtype] => 11 ) [26] => Array ( [titre1] => Accueil [parent] => 0 [idd] => 26 [add] => 3 [mod] => 3 [del] => 0 [adds] => 0 [mods] => 3 [dels] => 3 [dtype] => 12 ) [43] => Array ( [titre1] => Infolettre [parent] => 11 [idd] => 43 [add] => 0 [mod] => 0 [del] => 0 [adds] => 0 [mods] => 0 [dels] => 0 [dtype] => 11 ) [45] => Array ( [titre1] => Site Internet [parent] => 26 [idd] => 45 [add] => 0 [mod] => 3 [del] => 3 [adds] => 0 [mods] => 0 [dels] => 0 [dtype] => 11 ) [46] => Array ( [titre1] => Site Internet [parent] => 26 [idd] => 46 [add] => 0 [mod] => 3 [del] => 3 [adds] => 0 [mods] => 0 [dels] => 0 [dtype] => 11 ) [47] => Array ( [titre1] => Site Internet [parent] => 26 [idd] => 47 [add] => 0 [mod] => 3 [del] => 3 [adds] => 0 [mods] => 0 [dels] => 0 [dtype] => 0 ) [48] => Array ( [titre1] => Site Internet [parent] => 26 [idd] => 48 [add] => 0 [mod] => 3 [del] => 3 [adds] => 0 [mods] => 0 [dels] => 0 [dtype] => 0 ) [50] => Array ( [titre1] => Téléchargement [parent] => 0 [idd] => 50 [add] => 3 [mod] => 0 [del] => 0 [adds] => 0 [mods] => 3 [dels] => 3 [dtype] => 19 ) [51] => Array ( [titre1] => Skype [parent] => 50 [idd] => 51 [add] => 0 [mod] => 3 [del] => 3 [adds] => 0 [mods] => 0 [dels] => 0 [dtype] => 11 ) [52] => Array ( [titre1] => TeamViewer [parent] => 50 [idd] => 52 [add] => 0 [mod] => 3 [del] => 3 [adds] => 0 [mods] => 0 [dels] => 0 [dtype] => 0 ) I have an array that looks like this. I need to count how much data there is in each parent. Ex: id 1 has parent 0 id 2 has parent 1 id 3 has parent 1 so the code must return 2 for id 1, 0 for id 2 and 0 for id 3 How the hell do i do that lol Link to comment https://forums.phpfreaks.com/topic/204738-count-in-array/ Share on other sites More sharing options...
plutomed Posted June 14, 2010 Share Posted June 14, 2010 echo count($array); Link to comment https://forums.phpfreaks.com/topic/204738-count-in-array/#findComment-1071892 Share on other sites More sharing options...
Adam Posted June 14, 2010 Share Posted June 14, 2010 $totals = array(); foreach ($array as $key => $child) { $totals[$key] = $child['parent']; } print_r($totals); Edit: Wait, I don't think I understand what you mean by "count how much data there is in each parent"? Link to comment https://forums.phpfreaks.com/topic/204738-count-in-array/#findComment-1071897 Share on other sites More sharing options...
Adam Posted June 14, 2010 Share Posted June 14, 2010 Got it, rushed in before thinking you wanted the value of the parent. Here we go: $totals = array(); foreach ($array as $key => $child) { if ($child['parent'] == 0) { continue; } if (isset($totals[$child['parent']])) { $totals[$child['parent']]++; continue; } $totals[$child['parent']] = 1; } print_r($totals); Link to comment https://forums.phpfreaks.com/topic/204738-count-in-array/#findComment-1071902 Share on other sites More sharing options...
drisate Posted June 14, 2010 Author Share Posted June 14, 2010 Nope lol i need to count a specefic portion of the array. Ex: id 1 has parent 0 id 2 has parent 1 id 3 has parent 1 id 4 has parent 0 id 5 has parent 4 id 6 has parent 4 id 7 has parent 6 id 8 has parent 6 How can i return 2 for ID 6 (theres 2 child in it) when everything is in 1 big array. Link to comment https://forums.phpfreaks.com/topic/204738-count-in-array/#findComment-1071903 Share on other sites More sharing options...
Adam Posted June 14, 2010 Share Posted June 14, 2010 See my latest reply, think that's what you're after. Link to comment https://forums.phpfreaks.com/topic/204738-count-in-array/#findComment-1071904 Share on other sites More sharing options...
saviola Posted June 14, 2010 Share Posted June 14, 2010 $childNum = array(); function getPar($array, &$childNum) { foreach ( $array as $key => $value ) { if( $value['parent'] ) { if ( !isset($childNum[$value['parent']]) || !$childNum[$value['parent']] ) { $childNum[$value['parent']] = 1; } else { $childNum[$value['parent']]++ ; } } } return $childNum; } print_r(getPar($arr, $childNum)); Link to comment https://forums.phpfreaks.com/topic/204738-count-in-array/#findComment-1071915 Share on other sites More sharing options...
drisate Posted June 14, 2010 Author Share Posted June 14, 2010 thanks it's perfect. :-) One last thing. Is there a way to modify that function so it only returns the number i need? Ex: $nb_of_child = getPar($array, &$parArr, 0); // number of childs for id 0 Link to comment https://forums.phpfreaks.com/topic/204738-count-in-array/#findComment-1071920 Share on other sites More sharing options...
drisate Posted June 14, 2010 Author Share Posted June 14, 2010 Got it thx guys ;-) function getPar($array, &$parArr, $id) { foreach ( $array as $key => $value ) { if( $value['parent'] == $id) { if ( !isset($parArr[$value['parent']]) || !$parArr[$value['parent']] ) { $parArr[$value['parent']] = 1; } else { $parArr[$value['parent']]++ ; } } } return $parArr[$id]; } echo getPar($menu_array, $parArr, 0); Link to comment https://forums.phpfreaks.com/topic/204738-count-in-array/#findComment-1071925 Share on other sites More sharing options...
Adam Posted June 14, 2010 Share Posted June 14, 2010 You're just wasting resources calling that function every time. Why not just populate the array and access the total by it's key..? echo $totals[0]; Link to comment https://forums.phpfreaks.com/topic/204738-count-in-array/#findComment-1071933 Share on other sites More sharing options...
drisate Posted June 14, 2010 Author Share Posted June 14, 2010 your right lol thx haha Link to comment https://forums.phpfreaks.com/topic/204738-count-in-array/#findComment-1071972 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.