How to order the massif

How to order the massif

Ways of ordering of elements of massifs depend on tools which are available for you on hand. Several options of ordering of one-dimensional massifs when using the most widespread server PHP programming language are given below. To independently make functions for search of elements of the massif, their comparison and assignment of new values when using this language is not required - all this is done by the built-in functions.

Instruction

1. Use the sort function () if it is necessary to build data in the massif as their increase. For example: $values = array (58, 15, 2.41, 26, 30); sort ($values); As a result of application of function the order of arrangement of data in the massif will change - it will become such: (2.41, 15, 26, 30, 58). If to add SORT_STRING flag to a call of function, then function will consider data of the massif line variables and to build them alphabetically. As the first sign of the line 2.41 variable in the alphabet is located further, than the first sign of the line 15 variable, after sort function application ($values, SORT_STRING) variables will be built differently: (15, 2.41, 26, 30, 58).

2. Use the rsort function () if necessary to order the massif in decreasing order of its values. This function differs from the sorting described in the first step only by order.

3. Apply the asort function () if it is necessary to order in ascending order of value of the called (associative) massif, without changing at the same time initial compliances between the index and value of each element of the massif. For example: $values = array ('one' => 58, 'two' => 15, 'three' => 2.41, 'four' => 26, 'five' => 30); asort ($values); As a result the sequence of elements of the massif will become such: ('three' => 2.41, 'two' => 15, 'four' => 26, 'five' => 30, 'one' => 58). For the rest action of this function does not differ from the sort function described in the first step. For similar ordering of elements in decreasing order use the arsort function ().

4. Use the ksort function () if it is necessary to order elements in ascending order of not values, but indexes (keys). This function is relevant for the called (associative) massifs. For example: $values = array ('one' => 58, 'two' => 15, 'three' => 2.41, 'four' => 26, 'five' => 30); ksort ($values); As a result function keys will be alphabetically built, and together with them also the sequence of values will change: ('five' => 30, 'four' => 26, 'one' => 58, 'three' => 2.41, 'two' => 15). The krsort function is upside-down intended for ordering of keys ().

5. Use the array_reverse function () if just it is required to change a sequence of values of elements of the massif to opposite. That is to appropriate value of the last element of the massif to the first, penultimate - to the second, etc. For example: $values = array ('one' => 58, 'two' => 15, 'three' => 2.41, 'four' => 26, 'five' => 30); $newValues = array_reverse ($values); As a result in the massif of $newValues the elements will follow in such order: ('five' => 30, 'four' => 26, 'three' => 2.41, 'two' => 15, 'one' => 58). Pay attention that application of this function does not change a sequence of elements in the initial massif of $values.

Author: «MirrorInfo» Dream Team


Print