Created
July 25, 2011 03:03
-
-
Save MasterEx/1103483 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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