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

array_filter() public static method

Filters an array using a specified callback.
public static array_filter ( Context ctx, PhpArray array, IPhpCallable callback ) : PhpArray
ctx Pchp.Core.Context Current runtime context.
array Pchp.Core.PhpArray The array to be filtered.
callback IPhpCallable /// The callback called on each value in the . /// If the callback returns value convertible to true the value is copied to the resulting array. /// Otherwise, it is ignored. ///
return Pchp.Core.PhpArray
        public static PhpArray array_filter(Context ctx /*, caller*/, PhpArray array, IPhpCallable callback)
        {
            if (array == null)
            {
                //PhpException.ArgumentNull("array");
                //return null;
                throw new ArgumentNullException(nameof(array));
            }

            if (callback == null)
            {
                //PhpException.ArgumentNull("callback");
                //return null;
                throw new ArgumentNullException(nameof(callback));
            }

            var result = new PhpArray();
            var args = new PhpValue[1];

            var iterator = array.GetFastEnumerator();
            while (iterator.MoveNext())
            {
                var entry = iterator.Current;

                // no deep copying needed because it is done so in callback:
                args[0] = entry.Value;

                // adds entry to the resulting array if callback returns true:
                if (callback.Invoke(ctx, args).ToBoolean())
                {
                    result.Add(entry);
                }
            }

            // values should be inplace deeply copied:
            //result.InplaceCopyOnReturn = true;
            return result;
        }

Same methods

Arrays::array_filter ( PhpArray array ) : PhpArray