Skip to content

Instantly share code, notes, and snippets.

@weeking
Created November 2, 2014 11:02
Show Gist options
  • Save weeking/9eb9b5599ad02da8604b to your computer and use it in GitHub Desktop.
Save weeking/9eb9b5599ad02da8604b to your computer and use it in GitHub Desktop.
Daily #186 easy
<?php
$file = '186.txt';
$file = fopen($file, 'r');
$fileContent = array();
$sortedArray = array();
while(!feof($file))
{
array_push($fileContent, trim(preg_replace('/\s\s+/', ' ', fgets($file))));
}
fclose($file);
foreach($fileContent as $content)
{
if(array_key_exists($content, $sortedArray))
{
$sortedArray[$content] += 1;
} else
{
$sortedArray[$content] = 1;
}
}
echo 'Candy in the bag: ' . sizeof($fileContent) . "\n";
echo 'Candy by type and % of total: ' . "\n";
foreach($sortedArray as $key=>$value)
{
echo $key . ': ' . $value . '(' . (($value/sizeof($fileContent))*100) . '%)' . "\n";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment