Advertisement
rjangelov

StringsInPHP - //Problem 5. Custom Sorting

Jun 30th, 2014
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.77 KB | None | 0 0
  1. //Problem 5.    Custom Sorting
  2. //Write function that sort array according to the element with that index in the string. For example if given index is 2, sort array by //second index of string.  
  3. <!DOCTYPE html>
  4. <html>
  5.     <head>
  6.         <meta charset="UTF-8">
  7.         <title></title>
  8.     </head>
  9.     <body>
  10.         <form method="post" action="CustomString.php">
  11.             <input type="text" name="str1">
  12.             <input type="submit" value="GO">
  13.         </form>
  14.     </body>
  15. </html>
  16. <?php
  17. $int=1;//sorting index
  18. $myArr=array("ovca","kashta","agne","abreviatura","buldog", "123456789");
  19. foreach($myArr as $v){
  20.     $newArr[$v]=substr($v, $int);
  21. }
  22. asort($newArr);
  23. foreach($newArr as $k=>$vv){
  24.     $finalArr[]=$k;
  25. }
  26. echo '<pre>' . print_r($finalArr, true) . '</pre>';
  27. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement