使用$ key无序的foreach($ list as $ key => $ categoryTournament)

时间:2016-03-23 06:17:23

标签: php

我正在使用Laravel,但我认为这对PHP来说是通用的。

当我尝试使用此代码循环Collection时

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

输出: 1 0 2 4 3

我随机获取了解除键,而不是像我期望的那样拥有0 1 2 3 4

CategoryTournament是一个Object,我加入了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
    }
  ]
}

知道为什么会这样吗?

2 个答案:

答案 0 :(得分:0)

您可以使用ksort功能根据键

对数组进行排序
ksort($categoryTournaments); 

此功能按升序排列数组键

答案 1 :(得分:0)

列表无序是完全正常的。在PHP中是允许的。

如果你想要有序列表,你应该使用其中一个功能:ksort($categoryTournaments)如果可能的话。

如果不是:$categoryTournaments->sortBy('id'),其中id是您想要排序的键。 检查您提供的文档中的其他功能。