Php 带有无序$key的foreach($key=>;$categoryTournament)
我使用的是Laravel,但我认为这对PHP来说是通用的 当我尝试使用此代码循环集合时Php 带有无序$key的foreach($key=>;$categoryTournament),php,Php,我使用的是Laravel,但我认为这对PHP来说是通用的 当我尝试使用此代码循环集合时 @foreach($categoryTournaments as $key => $categoryTournament) {{ $key }} // Means echo @endforeach 输出:120243 我随机获得无序的键,而不是像我预期的那样拥有01234 CategoryTournament是一个对象,我加入了5个对象: Collection {#522 ▼ #items:
@foreach($categoryTournaments as $key => $categoryTournament)
{{ $key }} // Means echo
@endforeach
输出:120243
我随机获得无序的键,而不是像我预期的那样拥有01234
CategoryTournament是一个对象,我加入了5个对象:
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
}
]
}
知道为什么会这样吗?您可以使用
ksort
函数根据键对数组进行排序
ksort($categoryTournaments);
此函数按升序排列数组键列表无序实际上是完全正常的。在PHP中是允许的 如果您想要有一个有序列表,您应该使用以下函数之一:
ksort($categoryTournaments)
(如果可能的话)
如果不是:$categoryTournaments->sortBy('id')
,其中id是要排序的键。
检查您提供的文档中的其他函数。您能说出Collection类的确切含义吗?可能它的迭代器接口实现包含一些自定义逻辑。@JuliatzindelToro将此函数添加到foreach循环之上