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

array_flip() public static method

Swaps all keys and their associated values in an array.

Values which are not of type string nor int are skipped and for each such value a warning is reported. If there are more entries with the same value in the array the last key is considered others are ignored. String keys are converted using Core.Convert.StringToArrayKey.

Unlike PHP this method doesn't return false on failure but a null reference. This is because it fails only if array is a null reference.

is a null reference (Warning). A value is neither nor (Warning).
public static array_flip ( PhpArray array ) : PhpArray
array Pchp.Core.PhpArray The array.
return Pchp.Core.PhpArray
        public static PhpArray array_flip(PhpArray array)
        {
            if (array == null)
            {
                //PhpException.ArgumentNull("array");
                //return null;
                throw new ArgumentNullException();
            }

            PhpArray result = new PhpArray(array.Count);

            var iterator = array.GetFastEnumerator();
            while (iterator.MoveNext())
            {
                var entry = iterator.Current;
                // dereferences value:
                var val = entry.Value.GetValue();
                switch (val.TypeCode)
                {
                    case PhpTypeCode.Int32:
                    case PhpTypeCode.Long:
                    case PhpTypeCode.String:
                    case PhpTypeCode.WritableString:
                        var askey = val.ToIntStringKey();
                        result[askey] = PhpValue.Create(entry.Key);
                        break;
                    default:
                        // PhpException.Throw(PhpError.Warning, LibResources.GetString("neither_string_nor_integer_value", "flip"));
                        throw new ArgumentException();
                }
            }

            // no need to deep copy because values are ints/strings only (<= keys were int/strings only):
            return result;
        }