Core Function Order

From Sputnik Wiki
Jump to: navigation, search
Order( <array>, <flag> )

Contents

Description

Fix the order of numeric array elements from 0 to whatever.

Parameters

array

The array to order.

flag

Optional; If the flag is TRUE then a copy of the array but ordered will be returned and the original will be unmodified.

Default: False (Modify the actual array given)

Return Value

Nothing unless Flag is TRUE then it will return a new array (On success) or empty array (On failure).

Remarks

Because of the way Sputnik handles arrays they wont always be in perfect order from 0 1 2 3 4 etc etc for example if you use unset( $array[3] ) it will just delete element ID 3 and leave everything else as it is so there will be a gap in your index.

Functions that modify the array will also leave such gaps.

There are exceptions though push, insert, unshift etc etc all move the index properly (push wont reorder the array but it will append to the LAST index it finds) and leave no gaps.

To solve this issue you can use the Order() function.

This function modifies an array on the spot and does not return anything at all.

It will take all arrays elements and place them in order they are found so you will get a proper index 0, 1, 2, 3, 4, 5 and so on regardless of what indexes the elements had when you called Order().

Its recommended to only order an array if you have to since it can be a resource intensive task depending on the size of the array.

Example

$Array = array("Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight");
unset( $Array[1] ); // Delete element at index 1
printr($Array);
/*
Prints:
ARRAY
{
        [0] => Zero
        [2] => Two
        [3] => Three   <- Notice IDs arent consistent
        [4] => Four       now that an item was deleted?
        [5] => Five
        [6] => Six
        [7] => Seven
        [8] => Eight
}
*/
Order($Array); // Fix the order of the indexes
printr($Array);
/*
Prints:
ARRAY
{
        [0] => Zero
        [1] => Two
        [2] => Three   <- Notice IDs are fixed now?
        [3] => Four
        [4] => Five
        [5] => Six
        [6] => Seven
        [7] => Eight
}
*/
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox