Welcome to the Webmaster Forum.
Results 1 to 5 of 5

Thread: Sorting Arrays

  1. #1
    Senior Member
    Join Date
    Jan 2014
    Location
    Banglore
    Posts
    289

    Sorting Arrays

    One of the most useful features of arrays is that PHP can sort them for you. PHP originally stores array elements in the order in which you create them.If you display the entire array without changing the order, the elements are displayed in the order in which they were created. Often, you want to change this order. For example, you may want to display the array in alphabetical order by value or by key.




    PHP can sort arrays in a variety of ways. To sort an array that has numbers as keys, use a sort statement as follows

    sort($arrayname);
    This statement sorts arrays by the values and assigns new keys that are the appropriate numbers. The values are sorted with numbers first, uppercase letters next, and lowercase letters last. For example, consider the $streets
    array:

    $streets[0] = “Elm St.”;
    $streets[1] = “Oak Dr.”;
    $streets[2] = “7th Ave.”;
    You enter the following sort statement:
    sort($streets);
    Now the array becomes as follows:

    $streets[0] = “7th Ave.”;
    $streets[1] = “Elm St.”;
    $streets[2] = “Oak Dr.”;
    If you use sort() to sort an array with words as keys, the keys are changed to numbers, and the word keys are thrown away.

    To sort arrays that have words for keys, use the asort statement as follows:

    asort($capitals);
    This statement sorts the capitals by value, but it keeps the original key for each value instead of assigning a number key. For example, consider the state capitals array created in the preceding section:

    $capitals[‘CA’] = “Sacramento”;
    $capitals[‘TX’] = “Austin”;
    $capitals[‘OR’] = “Salem”;
    You use the following asort statement,
    asort($capitals);
    The array becomes as follows:

    $capitals[‘TX’] = Austin
    $capitals[‘CA’] = Sacramento
    $capitals[‘OR’] = Salem
    Notice that the keys stayed with the value when the elements were reordered. Now the elements are in alphabetical order, and the correct state key is still with the appropriate state capital. If the keys has been numbers, the numbers would now be in a different order. For example, suppose the original array was as follows:

    $capitals[1] = “Sacramento”;
    $capitals[2] = “Austin”;
    $capitals[3] = “Salem”;
    After an asort statement, the new array would be as follows:

    $capitals[2] = Austin
    $capitals[1] = Sacramento
    $capitals[3] = Salem
    It’s unlikely that you want to use asort on an array with numbers as a key.
    Last edited by chinmay.sahoo; 12-28-2015 at 01:24 AM.

  2. #2
    Senior Member
    Join Date
    Nov 2015
    Posts
    405

    Re: Sorting Arrays

    thanks very much

  3. #3
    Member
    Join Date
    Jan 2016
    Posts
    31

    Re: Sorting Arrays

    so cool , thanks

  4. #4
    Senior Member
    Join Date
    Jan 2016
    Location
    United State
    Posts
    176

    Re: Sorting Arrays

    Awesome! Thank you very much!

  5. #5
    Senior Member
    Join Date
    Dec 2015
    Location
    Mumbai
    Posts
    385

    Re: Sorting Arrays

    Very nice information thanks for sharing.

    Rentalproxies

 

 

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Back to top

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203