【问题标题】:How to remove duplicate values in an array while printing the other values in the duplicated element如何在打印重复元素中的其他值时删除数组中的重复值
【发布时间】:2019-02-07 21:22:17
【问题描述】:

我有一个查询的输出(从控制器传递到视图),它被转换为如下所示的 JSON 数组。

array (size=14)
  0 => 
    array (size=4)
      'student_id' => string '8660' (length=4)
      'session_count' => int 6
      'type' => string 'lecture' (length=7)
      'group' => null
  1 => 
    array (size=4)
      'student_id' => string '8660' (length=4)
      'session_count' => int 5
      'type' => string 'practical' (length=9)
      'group' => string 'A' (length=1)
  2 => 
    array (size=4)
      'student_id' => string '8661' (length=4)
      'session_count' => int 7
      'type' => string 'lecture' (length=7)
      'group' => null
  3 => 
    array (size=4)
      'student_id' => string '8661' (length=4)
      'session_count' => int 3
      'type' => string 'practical' (length=9)
      'group' => string 'B' (length=1)
  4 => 
    array (size=4)
      'student_id' => string '8662' (length=4)
      'session_count' => int 4
      'type' => string 'lecture' (length=7)
      'group' => null

我想在表格中打印 Student_id Session Count(Lectures) Session Count(Practical) (这样每个学生编号仅与相关的 Lectures 和 Practical 会话计数一起出现在一行中)。 我广泛搜索解决方案。此论坛中所有可用于删除重复项的解决方案也会删除整个数组元素,从而删除其中包含的讲座/实践和会话计数值。我的@foreach 循环打印重复。

如何以上述格式打印此内容,同时在表格中打印唯一的 student_ids 以及相关的会话计数(讲座)会话计数(实践)。我正在使用 Laravel 4.2。

【问题讨论】:

    标签: php arrays


    【解决方案1】:

    您必须首先在 for-each 循环之外创建一个空数组,然后首先检查创建的数组中是否存在 student_id,如果不存在则将 student_id 推送到数组中,如果不存在则继续循环而不打印详细信息。

    【讨论】:

      猜你喜欢
      • 2017-12-16
      • 2021-05-03
      • 2012-06-09
      • 2014-01-27
      • 2013-03-23
      • 2015-09-24
      • 2017-08-12
      • 2018-02-14
      • 1970-01-01
      相关资源
      最近更新 更多