jacko_162 Posted November 13, 2012 Share Posted November 13, 2012 I have the following link; http://eve-kill.net/epic/involvedCorp:Skulldogs/mask:64/mailLimit:5 which pulls an array of victimCorpName and limits the results to 5. how can i get it to list these in a bullet point list and only show the VictimCorpName for example: victimCorpName victimCorpName victimCorpName victimCorpName victimCorpName and if i remove the "limit" is it possible to count the amount of times a certain "victimCorpName" comes up, for example: FSB-Alpha: x7 i tried to use the print_r(&myArray) but to no avail. any help would be appreciated. this is my current code theory: <?php $file_contents = file_get_contents('http://eve-kill.net/epic/involvedCorp:Skulldogs/mask:64/mailLimit:5'); $decode = json_decode($file_contents, true); $encode = json_encode($file_contents); //echo $data[2]->victimCorpName; //echo $data->title; print_r($decode); ?> Quote Link to comment https://forums.phpfreaks.com/topic/270629-converting-feed-into-readable-list/ Share on other sites More sharing options...
Barand Posted November 13, 2012 Share Posted November 13, 2012 try <?php $file_contents = file_get_contents('http://eve-kill.net/epic/involvedCorp:Skulldogs/mask:64/mailLimit:5'); $decode = json_decode($file_contents, true); $arr = array(); foreach ($decode as $d) { $arr[] = $d['victimCorpName']; } $counts = array_count_values($arr); echo '<pre>'.print_r($counts, 1).'</pre>'; ?> RESULTS Array ( [FSB-ALFA] => 3 [Ministry of War] => 1 [solar Wind] => 1 ) Quote Link to comment https://forums.phpfreaks.com/topic/270629-converting-feed-into-readable-list/#findComment-1392040 Share on other sites More sharing options...
jacko_162 Posted November 13, 2012 Author Share Posted November 13, 2012 The count works great, is it possible to list them using bullet points without the "array ()" and "[]" ? Quote Link to comment https://forums.phpfreaks.com/topic/270629-converting-feed-into-readable-list/#findComment-1392043 Share on other sites More sharing options...
Barand Posted November 13, 2012 Share Posted November 13, 2012 foreach Quote Link to comment https://forums.phpfreaks.com/topic/270629-converting-feed-into-readable-list/#findComment-1392048 Share on other sites More sharing options...
jacko_162 Posted November 14, 2012 Author Share Posted November 14, 2012 works great. Thank you, ok and perhaps my last question, the foreach() works and i got my list. on your count code, how can i get it to add up all the amounts? and give a total? for example: Array ( [FSB-ALFA] => 3 [Ministry of War] => 1 [solar Wind] => 1 ) Total: 5 Quote Link to comment https://forums.phpfreaks.com/topic/270629-converting-feed-into-readable-list/#findComment-1392328 Share on other sites More sharing options...
Barand Posted November 14, 2012 Share Posted November 14, 2012 array_sum Quote Link to comment https://forums.phpfreaks.com/topic/270629-converting-feed-into-readable-list/#findComment-1392330 Share on other sites More sharing options...
jacko_162 Posted November 14, 2012 Author Share Posted November 14, 2012 i looked at array_sum but i cant seem to get it to add correctly: <?php $file_contents = file_get_contents('http://eve-kill.net/epic/involvedCorp:Skulldogs/mask:16/mailLimit:10'); $decode = json_decode($file_contents, true); $arr = array(); foreach ($decode as $d) { $arr[] = $d['victimName']; } $counts = array_count_values($arr); $sum = array_sum($decode); echo '<pre>'.print_r($counts, 1).'</pre>'; echo "<br><br><br>SUM: "; echo "" . $sum . "\n"; ?> it outputs: Array ( [R4K-8L] => 1 [Calette Zardina] => 1 [sylouis] => 1 [sylia Nimlas] => 1 [Tcpp24] => 2 [Greg Focker Gaylord] => 2 [PMV-G6 VII - Moon 4] => 2 ) SUM: 0 it should give me SUM: 10 Quote Link to comment https://forums.phpfreaks.com/topic/270629-converting-feed-into-readable-list/#findComment-1392338 Share on other sites More sharing options...
jacko_162 Posted November 14, 2012 Author Share Posted November 14, 2012 oops, $sum = array_sum($decode); should be: $sum = array_sum($count); Quote Link to comment https://forums.phpfreaks.com/topic/270629-converting-feed-into-readable-list/#findComment-1392339 Share on other sites More sharing options...
Barand Posted November 14, 2012 Share Posted November 14, 2012 Yes, that's the on to sum. You have something like this by now <?php $file_contents = file_get_contents('http://eve-kill.net/epic/involvedCorp:Skulldogs/mask:64/mailLimit:10'); $decode = json_decode($file_contents, true); $arr = array(); foreach ($decode as $d) { $arr[] = $d['victimCorpName']; } $counts = array_count_values($arr); /************************* * bullet list **************************/ echo "<ul>\n"; foreach ($counts as $name=>$n) { echo "<li>$name ($n)</li>\n"; } echo "</ul>\n"; /****** * Total *******/ echo "<br />Total : " . array_sum($counts); ?> Quote Link to comment https://forums.phpfreaks.com/topic/270629-converting-feed-into-readable-list/#findComment-1392341 Share on other sites More sharing options...
Barand Posted November 14, 2012 Share Posted November 14, 2012 BTW, the total should always be the same value as your mailLimit attribute in the URL Quote Link to comment https://forums.phpfreaks.com/topic/270629-converting-feed-into-readable-list/#findComment-1392348 Share on other sites More sharing options...
jacko_162 Posted November 14, 2012 Author Share Posted November 14, 2012 BTW, the total should always be the same value as your mailLimit attribute in the URL yeah i was checking that against the result, hence why i noticed it was wrong. and thank you for the formatting above. looks better..! say if i pull a result of $sum as 1000000 (how can i output this as "1 Million", or 1350000 as "1.35 Million, or 350000 as "0.35 Million" etc..) i looked at number_format() but to no avail. Quote Link to comment https://forums.phpfreaks.com/topic/270629-converting-feed-into-readable-list/#findComment-1392353 Share on other sites More sharing options...
Manixat Posted November 14, 2012 Share Posted November 14, 2012 I believe there is no such function, I might be wrong though. Anyway you can write up your own function ^^ Quote Link to comment https://forums.phpfreaks.com/topic/270629-converting-feed-into-readable-list/#findComment-1392358 Share on other sites More sharing options...
jacko_162 Posted November 14, 2012 Author Share Posted November 14, 2012 After a quick google search i found the following function: function Humanize($size) { $size = preg_replace('/[^0-9]/','',$size); $sizes = array("", "K", "M", "B"); if ($size == 0) { return('n/a'); } else { return (round($size/pow(1000, ($i = floor(log($size, 1000)))), 0) . $sizes[$i]); } } echo "<br><br>Total:" . Humanize($result); it now outputs the short form, but doesnt include decimal places (for example it shows 33,175,961,290 as 33B, whereas i want it to show as 33.1B) any clues to alter? Quote Link to comment https://forums.phpfreaks.com/topic/270629-converting-feed-into-readable-list/#findComment-1392369 Share on other sites More sharing options...
jacko_162 Posted November 14, 2012 Author Share Posted November 14, 2012 After a quick google search i found the following function: function Humanize($size) { $size = preg_replace('/[^0-9]/','',$size); $sizes = array("", "K", "M", "B"); if ($size == 0) { return('n/a'); } else { return (round($size/pow(1000, ($i = floor(log($size, 1000)))), 0) . $sizes[$i]); } } echo "<br><br>Total:" . Humanize($result); it now outputs the short form, but doesnt include decimal places (for example it shows 33,175,961,290 as 33B, whereas i want it to show as 33.1B) any clues to alter? actually thinking about it it should work as is. thanks for help guys.. Quote Link to comment https://forums.phpfreaks.com/topic/270629-converting-feed-into-readable-list/#findComment-1392371 Share on other sites More sharing options...
Barand Posted November 14, 2012 Share Posted November 14, 2012 If you need decimal places, alter the hightlight parameter to the number required: return (round($size/pow(1000, ($i = floor(log($size, 1000)))), 3) . $sizes[$i]); Quote Link to comment https://forums.phpfreaks.com/topic/270629-converting-feed-into-readable-list/#findComment-1392383 Share on other sites More sharing options...
jacko_162 Posted November 14, 2012 Author Share Posted November 14, 2012 (edited) If you need decimal places, alter the hightlight parameter to the number required: return (round($size/pow(1000, ($i = floor(log($size, 1000)))), 3) . $sizes[$i]); aaah many thanks, another quick one if you can? say i pull a feed like above in my original posts, and have a limit of 25, then count them and have a list like so: echo '<pre>'.print_r($counts, 1).'</pre>'; Outputs: [FSB-ALFA] => 9 [Ministry of War] => 7 [solar Wind] => 5 [FSB-ALFA] => 3 [Ministry of War] => 1 [solar Wind] => 1 is it possible to only echo the top 1 (with most counts) instead of the whole array: [FSB-ALFA] => 9 Edited November 14, 2012 by jacko_162 Quote Link to comment https://forums.phpfreaks.com/topic/270629-converting-feed-into-readable-list/#findComment-1392450 Share on other sites More sharing options...
jacko_162 Posted November 14, 2012 Author Share Posted November 14, 2012 using this code by the way: <?php $file_contents = file_get_contents('http://eve-kill.net/epic/involvedCorp:Skulldogs/mask:64/mailLimit:10'); $decode = json_decode($file_contents, true); $arr = array(); foreach ($decode as $d) { $arr[] = $d['victimCorpName']; } $counts = array_count_values($arr); /************************* * bullet list **************************/ echo "<ul>\n"; foreach ($counts as $name=>$n) { echo "<li>$name ($n)</li>\n"; } echo "</ul>\n"; /****** * Total *******/ echo "<br />Total : " . array_sum($counts); ?> which outputs nicely formatted: Fleet of the Eternal Night (3) A0 Inc (1) The Curse of Distant Stars (2) Lamb Federation Navy (1) Deep Space Supplies (1) LdW Industries (1) Silent Sentinels (1) Total : 10 looking at the above it doesnt sort them in DESC order... so i would need it in decending order and to limit it to the top 1 or 2 Quote Link to comment https://forums.phpfreaks.com/topic/270629-converting-feed-into-readable-list/#findComment-1392453 Share on other sites More sharing options...
Barand Posted November 14, 2012 Share Posted November 14, 2012 I added a couple of lines - with comments <?php $file_contents = file_get_contents('http://eve-kill.net/epic/involvedCorp:Skulldogs/mask:64/mailLimit:10'); $decode = json_decode($file_contents, true); $arr = array(); foreach ($decode as $d) { $arr[] = $d['victimCorpName']; } $counts = array_count_values($arr); arsort($counts); // sort in DESC order $topcounts = array_slice ($counts, 0, 2, true); // the "2" dictates how many to show /************************* * bullet list **************************/ echo "<ul>\n"; foreach ($topcounts as $name=>$n) { echo "<li>$name ($n)</li>\n"; } echo "</ul>\n"; /****** * Total *******/ echo "<br />Total : " . array_sum($counts); ?> Quote Link to comment https://forums.phpfreaks.com/topic/270629-converting-feed-into-readable-list/#findComment-1392456 Share on other sites More sharing options...
jacko_162 Posted November 14, 2012 Author Share Posted November 14, 2012 thank you my friend... all seems to work lovely. Quote Link to comment https://forums.phpfreaks.com/topic/270629-converting-feed-into-readable-list/#findComment-1392466 Share on other sites More sharing options...
jacko_162 Posted November 15, 2012 Author Share Posted November 15, 2012 (edited) hmmm im getting the following error; Warning: array_count_values() [function.array-count-values]: Can only count STRING and INTEGER values! in /homepages/*****/feed/topKillers.php on line 10 using the below code: <?php $file_contents = file_get_contents('http://eve-kill.net/epic/involvedCorp:Skulldogs/mask:524288/mailLimit:1'); $decode = json_decode($file_contents, true); $arr = array(); foreach ($decode as $d) { $arr[] = $d['characterName']; } $counts = array_count_values($arr); //error occurs here echo '<pre>'.print_r($counts, 1).'</pre>'; ?> is there a work around to be able to list and count the "characterName" without the errors? Edited November 15, 2012 by jacko_162 Quote Link to comment https://forums.phpfreaks.com/topic/270629-converting-feed-into-readable-list/#findComment-1392503 Share on other sites More sharing options...
Barand Posted November 15, 2012 Share Posted November 15, 2012 (edited) The returned array structure is completely diferrent from that returned with your previous mask value. You need to adjust your code accordingly. Although this may need changing if limit increased. <?php $file_contents = file_get_contents('http://eve-kill.net/epic/involvedCorp:Skulldogs/mask:524288/mailLimit:1'); $decode = json_decode($file_contents, true); echo '<pre>'.print_r($decode, 1).'</pre>'; // view new structure $arr = array(); foreach ($decode[0]['involved'] as $d) { //process new structure $arr[] = $d['characterName']; } $counts = array_count_values($arr); //error occurs here echo '<pre>'.print_r($counts, 1).'</pre>'; ?> Edited November 15, 2012 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/270629-converting-feed-into-readable-list/#findComment-1392506 Share on other sites More sharing options...
jacko_162 Posted November 16, 2012 Author Share Posted November 16, 2012 The returned array structure is completely diferrent from that returned with your previous mask value. You need to adjust your code accordingly. Although this may need changing if limit increased. <?php $file_contents = file_get_contents('http://eve-kill.net/epic/involvedCorp:Skulldogs/mask:524288/mailLimit:1'); $decode = json_decode($file_contents, true); echo '<pre>'.print_r($decode, 1).'</pre>'; // view new structure $arr = array(); foreach ($decode[0]['involved'] as $d) { //process new structure $arr[] = $d['characterName']; } $counts = array_count_values($arr); //error occurs here echo '<pre>'.print_r($counts, 1).'</pre>'; ?> using the above structure, it only echos and counts from the array [0] if that makes sense, i will change the "mailLimit" to 500 eventually and i need to to show the top 10 chacterNames and the amount of times that character appears like so: [scalira] => 10 [Rip1ey] => 9 [scalira] => 8 [Rip1ey] => 7 [scalira] => 6 [Rip1ey] => 5 [scalira] => 4 [Rip1ey] => 3 [Rip1ey] => 2 [Rip1ey] => 1 Quote Link to comment https://forums.phpfreaks.com/topic/270629-converting-feed-into-readable-list/#findComment-1392962 Share on other sites More sharing options...
Barand Posted November 16, 2012 Share Posted November 16, 2012 I did warn you that it would changing with larger limits Quote Link to comment https://forums.phpfreaks.com/topic/270629-converting-feed-into-readable-list/#findComment-1393012 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.