我有必須以字母順序排序字母陣列SplObjectStorage() set使用SPL方法如rewind(),current(),next(),和valid()。目前,我的while回圈無限期地運行而不進行任何排序,但我不確定為什么。我還沒有在網上看到任何與排序 SPL 雙向鏈表或物件存盤相關的內容,所以希望這對 SOC 有用。
<?php
$letters = ["b", "a", "c", "e", "f", "d"];
$setLetters = new SplObjectStorage();
foreach ($letters as $key => $value) {
$o = new stdClass();
$o->$key = $value;
$setLetters->attach($o);
}
function printList($list)
{
for ($list->rewind(); $list->valid(); $list->next()) {
$k = $list->key();
echo ($list->current()->$k);
echo "<br>";
}
}
printList($setLetters); // ["b", "a", "c", "e", "f", "d"]
$sortedLetters = $setLetters;
function sortList($list)
{
$list->rewind();
$current = $list->current();
$currentK = $list->key();
$list->next();
while ($list->valid()) {
$nextK = $list->key();
if ($current->$currentK > $list->current()->$nextK) {
// [current.element, current.next.element] = [current.next.element, current.element];
$list->offsetSet($current, $list->current());
$list->offsetSet($list->current(), $current);
$list->rewind();
continue;
}
$current = $list->current();
}
}
sortList($sortedLetters);
printList($sortedLetters); //should return ["a", "b", "c", "d", "e", "f"]
?>
uj5u.com熱心網友回復:
我不認為你可以對存盤物件中的物件進行排序 - 至少我沒有找到一個快速修復,我認為在一段時間內回圈會很昂貴......
所以這里有一個簡單的解決方案:把所有的東西都拿出來,把它們分類,放回去。
注意:我添加了一些評論,讓您知道什么是錯誤的。 如果您能夠創建存盤,那么我建議使用“屬性”或“字母”或 w/e 作為屬性,而不是字母陣列中的當前數字鍵。
注意:我->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解決方案(沒有評論)。
$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";
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/334920.html
上一篇:為幾個物件制作靈活的文本
下一篇:如何解決未捕獲的型別錯誤:嘗試使用JS和html顯示當前時間時,無法在列印時間設定null的屬性(設定“innerHTML”)
