Skip to content

Instantly share code, notes, and snippets.

@MasterEx
Created July 25, 2011 03:03
Show Gist options
  • Save MasterEx/1103483 to your computer and use it in GitHub Desktop.
Save MasterEx/1103483 to your computer and use it in GitHub Desktop.
<?php
$input = "this is my sample which is sorted and simple";
echo "Input: ".$input."<br/>";
foreach(explode(" ",$input) as $in)
{
if(isset($firstarray[strlen($in)][$in]))
$firstarray[strlen($in)][$in]++;
else
$firstarray[strlen($in)][$in] = 1;
ksort($firstarray[strlen($in)]);
}
ksort($firstarray);
print_r($firstarray);
?>
<?php
/**
* Better use arrays by reference
* http://php.net/manual/en/language.references.php
*
*/
$input = "this is my sample which is sorted and simple";
echo "Input: ".$input."<br/>";
foreach(explode(" ",$input) as $in)
{
if(isset($firstarray[strlen($in)][$in]))
$firstarray[strlen($in)][$in]++;
else
$firstarray[strlen($in)][$in] = 1;
}
ksort($firstarray);
foreach($firstarray as &$secarray)
{
ksort($secarray);
}
print_r($firstarray);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment