本文整理汇总了C#中PHP.Core.PhpArray.GetBaseEnumerator方法的典型用法代码示例。如果您正苦于以下问题:C# PhpArray.GetBaseEnumerator方法的具体用法?C# PhpArray.GetBaseEnumerator怎么用?C# PhpArray.GetBaseEnumerator使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PHP.Core.PhpArray
的用法示例。
在下文中一共展示了PhpArray.GetBaseEnumerator方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Slice
public static PhpArray Slice(PhpArray array, int offset, int length, bool preserveKeys)
{
if (array == null)
{
PhpException.ArgumentNull("array");
return null;
}
// absolutizes range:
PhpMath.AbsolutizeRange(ref offset, ref length, array.Count);
var iterator = array.GetBaseEnumerator();
// moves iterator to the first item of the slice;
// starts either from beginning or from the end (which one is more efficient):
if (offset < array.Count - offset)
{
for (int i = -1; i < offset; i++) iterator.MoveNext();
}
else
{
for (int i = array.Count; i > offset; i--) iterator.MovePrevious();
}
// copies the slice:
PhpArray result = new PhpArray(length);
int ikey = 0;
for (int i = 0; i < length; i++)
{
KeyValuePair<IntStringKey, object> entry = iterator.Current;
// integer keys are reindexed if preserveKeys is false, string keys are not touched:
if (entry.Key.IsString)
{
result.Add(entry.Key, entry.Value);
}
else
{
if (!preserveKeys)
result.Add(ikey++, entry.Value);
else
result.Add(entry.Key, entry.Value);
}
iterator.MoveNext();
}
result.InplaceCopyOnReturn = true;
return result;
}