doulong4169 2016-03-23 06:17
浏览 34

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

I'm using Laravel, but I think this is general to PHP.

When I try to loop a Collection with with this code

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

Output: 1 0 2 4 3

I randomly get unorderer keys, instead of having 0 1 2 3 4 as I should expect.

CategoryTournament is an Object, I join the 5 objects:

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

Any idea why is it happening?

  • 写回答

2条回答 默认 最新

  • donglu7816 2016-03-23 06:24
    关注

    You can Use ksort function to sort the Array According to key

    ksort($categoryTournaments); 
    

    This function Arrange your array keys in Ascending order

    评论

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制