我不认为你可以对存储对象中的对象进行排序 -
至少我没有找到快速解决方案,我认为在一段时间内循环会很昂贵...... .
所以这里有一个简单的解决方案:将所有物品从存储中取出,分类,放回原处。
注意:我添加了一些 cmets 让您知道出了什么问题。
如果您有能力创建存储,那么我建议使用“属性”或“字母”或 w/e 作为属性,而不是字母数组中的当前数字键。 p>
注意:我在底部添加了属性->letter 的解决方案。
// Note: using wild keys for testing reasons.
$letters = [0 => "b", 10 => "a", 22 => "c", 3 => "e", 44 => "f", "d"];
// Create the storage (YOUR VERSION).
// IMPORTANT: you are using $key from the letters array
// on the new stdClass - that will become a problem.
// You may want to set them all
// on the same key (aka property)
// like f.e. "attribute" or "letter", ... .
$list = new SplObjectStorage();
foreach ($letters as $key => $value) {
$o = new stdClass();
$o->{$key} = $value;
$list->attach($o);
}
# DISABLED - does not work. Reason: $key -problem mentioned above.
#function printList(\SplObjectStorage $list)
#{
# for ($list->rewind(); $list->valid(); $list->next()) {
# $k = $list->key();
# echo($list->current()->$k);
# echo "<br>";
# }
#}
/**
* @param SplObjectStorage $list
*/
function printList(\SplObjectStorage $list)
{
for (
$list->rewind(), $i = 0;
$i < $list->count();
$i++, $list->next()
) {
// Note: $key is the key from the storage ($list) -
// NOT from the $letters array.
$key = $list->key();
// Note: $value is a stdClass created above.
// We actually do not know the property (class->{property})
// to access the letter.
$object = $list->current();
// Fix to get the property.
$objectProperty = key(get_object_vars($object));
// /Fix
// Get the letter from the object.
$letter = $object->{$objectProperty};
echo "{$key} => {$letter}\r\n";
}
}
/**
* @param SplObjectStorage $list
*/
function sortList(\SplObjectStorage $list)
{
$objects = [];
for (
$list->rewind(), $i = 0;
$i < $list->count();
$i++, $list->next()
) {
$objects[] = $list->current();
}
$list->removeAll($list);
uasort($objects, function (stdClass $a, stdClass $b) {
// Fix to get the property.
$objectProperty = key(get_object_vars($a));
// /Fix
$aLetter = $a->{$objectProperty};
// Fix to get the property.
$objectProperty = key(get_object_vars($b));
// /Fix
$bLetter = $b->{$objectProperty};
if ($aLetter == $bLetter) {
return 0;
}
// a < b === asc ; a > b === desc
return ($aLetter < $bLetter) ? -1 : 1;
});
foreach ($objects as $object) {
$list->attach($object);
}
}
printList($list);
// 0 => b
// 1 => a
// 2 => c
// 3 => e
// 4 => f
// 5 => d
echo "------\r\n";
sortList($list);
printList($list);
// 0 => a
// 1 => b
// 2 => c
// 3 => d
// 4 => e
// 5 => f
echo "------\r\n";
这里是->letter 解决方案(没有 cmets)。
$letters = [0 => "b", 10 => "a", 22 => "c", 3 => "e", 44 => "f", "d"];
$list = new SplObjectStorage();
foreach ($letters as $key => $value) {
$o = new stdClass();
$o->letter = $value;
$list->attach($o);
}
/**
* @param SplObjectStorage $list
*/
function printList(\SplObjectStorage $list)
{
for (
$list->rewind(), $i = 0;
$i < $list->count();
$i++, $list->next()
) {
echo "{$list->key()} => {$list->current()->letter}\r\n";
}
}
/**
* @param SplObjectStorage $list
*/
function sortList(\SplObjectStorage $list)
{
$objects = [];
for (
$list->rewind(), $i = 0;
$i < $list->count();
$i++, $list->next()
) {
$objects[] = $list->current();
}
$list->removeAll($list);
uasort($objects, function (stdClass $a, stdClass $b) {
if ($a->letter == $b->letter) {
return 0;
}
// a < b === asc ; a > b === desc
return ($a->letter < $b->letter) ? -1 : 1;
});
foreach ($objects as $object) {
$list->attach($object);
}
}
printList($list);
// 0 => b
// 1 => a
// 2 => c
// 3 => e
// 4 => f
// 5 => d
echo "------\r\n";
sortList($list);
printList($list);
// 0 => a
// 1 => b
// 2 => c
// 3 => d
// 4 => e
// 5 => f
echo "------\r\n";