如何编写一个解决方案,让当前的 PHP 解释器 (5.4) 足够智能,只需执行大约 3-5 个副本而不是完整的逐项数组排序?
请注意,我知道一些将元素插入索引数组的方法。然而这并不能满足我的理解。例如在C++中,您可以使用 std::copy 或将结构或联合作为多元素数组游标来执行某些操作。
所以我想知道我是否以某种方式遵守 PHP 的规则,一个人可以使用什么语法在幕后拥有更接近于
将 [从某个索引到A 末尾的元素范围] 复制到 temp C
将 B 复制到 A[索引],
将 C 复制到 A[Index+count(B)]
比这个...
$MasterItemList = $Page[$CurrentPage]->GetItems(); /* Returns an array with 512 Items. */
$UpdateList = GetUpdatePage(); /* Returns multi-dimensional array such that:
$result[][0]=an index and
$result[][1]=a list of items */
foreach($UpdateList as $Update)
{ foreach($Update as $cursor => $ItemList)
{
$cursor=$cursor+0; //to int..
$numitems=count($ItemList);
if($ItemList[0]->NewAddition)
{
$BeforeUpdate=array_splice($MasterItemList,0, $cursor, true);
$AfterUpdate=array_splice($MasterItemList, $cursor+$numitems, 0);
$MasterItemList=array_merge($BeforeUpdate,$ItemList,$AfterUpdate);
$Page[$CurrentPage]->OffsetCorrection+=$numitems;
}
else
{
$i=0;
foreach($ItemList as $LineItem)
{
$MasterItemList[$cursor+$i] = $LineItem;
$i++;
}
}
}
}
如果我记下这些错误,请原谅我,让我知道,我会纠正它们。
也就是说,我不认为解释器可以使用适当的引用和范围,以便能够直接使用此方法执行逻辑。它已经是一个看起来非常昂贵的东西了.. 可以做些什么来为 PHP 做这个“正确的方法”?
例子:
// An Update List
Array(
[0] => Array(
[0] => 31
[1] => Array(
[1] => stdClass Object
(
[NewAddition] => false
[Name] => "********"
[Date] => 1364920943
[Active] => 1
.
.
.
)
[2] => stdClass Object
(
[NewAddition] => false
[Name] => "********"
[Date] => 1364920943
[Active] => 1
.
.
.
)
[3] => stdClass Object
(
[NewAddition] => false
[Name] => "********"
[Date] => 1364920943
[Active] => 1
.
.
.
)
)
)
)
并且MasterItemList
只是这些相同对象的数组(class Item
)。
需要注意的几点:
- 此数据仅在与此脚本相关的任何地方以纯顺序方式访问。
- 在这部分脚本中,只需要检查新插入集合中的第一项是否更新。套装中的所有物品都将永远是新的。
- 超过 512 的项目会自动调整到下一页加载。我可以调整页面大小以在数组排序性能和数据获取性能(异步缓冲)之间进行权衡。