2016-03-23 11 views
0

私はLaravelを使用していますが、これはPHPにとっては一般的だと思います。

私はこのコード

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

出力とでループにコレクションを試してみてください。私は、ランダムに代わり、私が期待するべきであるとして0 1 2 3 4を持つのunordererキーを取得1 0 2 4 3

。すべてのアイデアは、なぜそれが起こっている

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 
    } 
    ] 
} 

CategoryTournamentは、私は5オブジェクトに参加し、オブジェクトのですか?

+0

コレクションクラスが正確に何か言うことができますか?おそらく、反復子インターフェイスの実装にはカスタムロジックが含まれているかもしれません。 – kozlice

+0

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

答えて

0

あなたは昇順

+0

コレクションが配列ではなく、期待していません –

+0

@JuliatzindelToro foreachループの上にこの関数を追加します –

0

そのリストは順不同で、実際に完全に正常であるにあなたの配列のキーを配置し、キー

ksort($categoryTournaments); 

この機能により配列をソートするksort機能を使用することができます。 PHPでは許可されています。

注文リストをご希望の場合は、可能な場合はksort($categoryTournaments)のいずれかの機能を使用する必要があります。

そうでない場合:$categoryTournaments->sortBy('id')、ここでidは並べ替えたいキーです。 提供したドキュメント内の他の機能を確認してください。

関連する問題