Pchp.Library.Arrays.array_reverse C# (CSharp) Method

array_reverse() public static method

Returns array which elements are taken from a specified one in reversed order.
public static array_reverse ( PhpArray array, bool preserveKeys = false ) : PhpArray
array Pchp.Core.PhpArray The array to be reversed.
preserveKeys bool Whether keys should be left untouched. /// If set to false then integer keys are reindexed starting from zero.
return Pchp.Core.PhpArray
        public static PhpArray array_reverse(PhpArray array, bool preserveKeys = false)
        {
            if (array == null)
            {
                //PhpException.ReferenceNull("array");
                //return null;
                throw new ArgumentNullException();
            }

            PhpArray result = new PhpArray();

            var e = array.GetFastEnumerator();

            if (preserveKeys)
            {
                // changes only the order of elements:
                while (e.MoveNext())
                {
                    result.Prepend(e.CurrentKey, e.CurrentValue);
                }
            }
            else
            {
                // changes the order of elements and reindexes integer keys:
                int i = array.IntegerCount;
                while (e.MoveNext())
                {
                    var key = e.CurrentKey;
                    result.Prepend(key.IsString ? key : new IntStringKey(--i), e.CurrentValue);
                }
            }

            // if called by PHP languge then all items in the result should be inplace deeply copied:
            //result.InplaceCopyOnReturn = true;
            return result;
        }