2016-03-23 13 views
0

ich Laravel bin, aber ich denke, das ist zu PHP allgemein ist.

Wenn ich Schleife versuchen, eine Sammlung mit diesem Code

@foreach($categoryTournaments as $key => $categoryTournament) 
    {{ $key }} // Means echo 
@endforeach 

Ausgang:1 0 2 4 3

ich zufällig unorderer Schlüssel, statt 0 1 2 3 4 von, wie ich erwarten sollte.

CategoryTournament ist ein Objekt, ich die 5 Objekte verbinden:

Collection {#522 ▼ 
    #items: array:5 [▼ 
    0 => CategoryTournament {#523 ▼ 
     #dates: array:3 [▶] 
     #table: "category_tournament" 
     +timestamps: true 
     #fillable: array:2 [▶] 
     #connection: null 
     #primaryKey: "id" 
     #perPage: 15 
     +incrementing: true 
     #attributes: array:6 [▼ 
     "id" => 164 
     "tournament_id" => 71 
     "category_id" => 5 
     "created_at" => "2016-03-23 00:04:47" 
     "updated_at" => "2016-03-23 00:04:47" 
     "deleted_at" => null 
     ] 
     #original: array:6 [▶] 
     #relations: [] 
     #hidden: [] 
     #visible: [] 
     #appends: [] 
     #guarded: array:1 [▶] 
     #dateFormat: null 
     #casts: [] 
     #touches: [] 
     #observables: [] 
     #with: [] 
     #morphClass: null 
     +exists: true 
     +wasRecentlyCreated: false 
     #forceDeleting: false 
    } 
    1 => CategoryTournament {#524 ▼ 
     #dates: array:3 [▶] 
     #table: "category_tournament" 
     +timestamps: true 
     #fillable: array:2 [▶] 
     #connection: null 
     #primaryKey: "id" 
     #perPage: 15 
     +incrementing: true 
     #attributes: array:6 [▶] 
     #original: array:6 [▶] 
     #relations: [] 
     #hidden: [] 
     #visible: [] 
     #appends: [] 
     #guarded: array:1 [▶] 
     #dateFormat: null 
     #casts: [] 
     #touches: [] 
     #observables: [] 
     #with: [] 
     #morphClass: null 
     +exists: true 
     +wasRecentlyCreated: false 
     #forceDeleting: false 
    } 
    2 => CategoryTournament {#525 ▼ 
     #dates: array:3 [▶] 
     #table: "category_tournament" 
     +timestamps: true 
     #fillable: array:2 [▶] 
     #connection: null 
     #primaryKey: "id" 
     #perPage: 15 
     +incrementing: true 
     #attributes: array:6 [▶] 
     #original: array:6 [▶] 
     #relations: [] 
     #hidden: [] 
     #visible: [] 
     #appends: [] 
     #guarded: array:1 [▶] 
     #dateFormat: null 
     #casts: [] 
     #touches: [] 
     #observables: [] 
     #with: [] 
     #morphClass: null 
     +exists: true 
     +wasRecentlyCreated: false 
     #forceDeleting: false 
    } 
    3 => CategoryTournament {#526 ▼ 
     #dates: array:3 [▶] 
     #table: "category_tournament" 
     +timestamps: true 
     #fillable: array:2 [▶] 
     #connection: null 
     #primaryKey: "id" 
     #perPage: 15 
     +incrementing: true 
     #attributes: array:6 [▶] 
     #original: array:6 [▶] 
     #relations: [] 
     #hidden: [] 
     #visible: [] 
     #appends: [] 
     #guarded: array:1 [▶] 
     #dateFormat: null 
     #casts: [] 
     #touches: [] 
     #observables: [] 
     #with: [] 
     #morphClass: null 
     +exists: true 
     +wasRecentlyCreated: false 
     #forceDeleting: false 
    } 
    4 => CategoryTournament {#527 ▼ 
     #dates: array:3 [▶] 
     #table: "category_tournament" 
     +timestamps: true 
     #fillable: array:2 [▶] 
     #connection: null 
     #primaryKey: "id" 
     #perPage: 15 
     +incrementing: true 
     #attributes: array:6 [▶] 
     #original: array:6 [▶] 
     #relations: [] 
     #hidden: [] 
     #visible: [] 
     #appends: [] 
     #guarded: array:1 [▶] 
     #dateFormat: null 
     #casts: [] 
     #touches: [] 
     #observables: [] 
     #with: [] 
     #morphClass: null 
     +exists: true 
     +wasRecentlyCreated: false 
     #forceDeleting: false 
    } 
    ] 
} 

Jede Idee, warum ist es passiert?

+0

Können Sie sagen, was genau Collection-Klasse ist? Vielleicht enthält seine Implementierung der Iterator-Schnittstelle einige benutzerdefinierte Logik. – kozlice

+0

https://laravel.com/docs/master/collections –

Antwort

0

Sie können ksort Funktion können Sie die Array nach Schlüssel

ksort($categoryTournaments); 

Diese Funktion Ordnen Sie Ihre Array-Schlüssel in aufsteigender Reihenfolge

+0

nicht funktionierende Sammlung erwartet, nicht Array –

+0

@JuliatzindelToro diese Funktion über der foreach Schleife hinzuzufügen –

0

Es ist eigentlich ganz normal, dass die Liste ist ungeordnet sortieren. In PHP ist es erlaubt.

Wenn Sie geordnete Liste haben, sollten Sie eine der Funktionen verwenden: ksort($categoryTournaments) wenn das möglich ist.

Falls nicht: $categoryTournaments->sortBy('id'), wobei id Schlüssel ist, nach der Sie sortieren möchten. Überprüfen Sie andere Funktionen in der von Ihnen bereitgestellten Dokumentation.