【问题标题】:Creating a weekly array with multiple entries for each day为每一天创建一个包含多个条目的每周数组
【发布时间】:2023-04-01 09:42:01
【问题描述】:

我有一个 MySQL 查询,它返回这样的数据转储(对于此问题排除的其他列,预期和需要重复):

SessionID  DayofWeek    SessionDetails  Description
1          Sunday       1:00 PM         Foo
1          Sunday       1:00 PM         Foo
1          Sunday       1:00 PM         Foo
1          Sunday       1:00 PM         Foo
2          Monday       10:00 AM        Foo
2          Monday       10:00 AM        Foo
2          Monday       10:00 AM        Foo
2          Monday       10:00 AM        Foo
3          Monday       7:00 PM         Barr
3          Monday       7:00 PM         Barr
3          Monday       7:00 PM         Barr
3          Monday       7:00 PM         Barr

我正在尝试为每一天创建一个数组,并且只在其中存储一次不同的会话/时间,例如,

Sunday => Array (
       [SessionID] => 1
       [SessionDetails] => 1:00 PM
       [Description] => Foo
)
Monday=> Array (
       [SessionID] => 2
       [SessionDetails] => 10:00 AM
       [Description] => Foo
       [SessionID] => 3
       [SessionDetails] => 7:00 PM
       [Description] => Bar
)

我在foreach ($sqlresults as $row) 循环之前为每一天设置一个空白数组,例如$sunday=array(); $monday=array(); 等。

然后在我正在做的循环内:

      if (!in_array($row['SessionID'],$sunday)){
          $sunday[]=array($row['SessionID'],$row['Description'], $row['SessionDetails']);
}

它有点工作,但它没有检查 if (!in_array) 部分是否正确,而是将每个(重复的)SessionID 添加为一个新数组,例如,

print_r($sunday)

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => Foo
            [2] => 1 PM
        )
    [1] => Array
        (
            [0] => 1
            [1] => Foo
            [2] => 1 PM
        )
    [2] => Array
        (
            [0] => 1
            [1] => Foo
            [2] => 1 PM
        )
    [3] => Array
        (
            [0] => 1
            [1] => Barr
            [2] => 1 PM
    )
    [4] => Array
        (
            [0] => 2
            [1] => Barr
            [2] => 10 AM
        )

等等……

我尝试在添加之前检查数组中的 sessionID:

if (!in_array($row['SessionID'][],$sunday)){

但是“致命错误:无法使用 [] 进行读取”

所以我尝试了:

  if (!in_array($sunday[$row['SessionID']],$sunday)){

但是“注意:未定义的偏移量:1”

我尝试将其设置为关联值:

      if (!in_array($row['SessionID'],$sunday)){
  $sunday[]=array("SessionID"=>($row['SessionID']),"Description"=>($row['Description']), "Time"=>($row['SessionDetails']));
}

同样的问题,除了数组更容易阅读:

[0] => Array
    (
        [SessionID] => 1
        [Description] => Foo
        [Time] => 1 PM
    )
[1] => Array
    (
        [SessionID] => 1
        [Description] => Foo
        [Time] => 1 PM
    )

TLDR:如何在尝试添加到日期数组之前检查索引 [SessionID]?

【问题讨论】:

    标签: php mysql arrays multidimensional-array


    【解决方案1】:

    这是使用array_unique 的简单方法。

    $result = [];
    
    foreach (array_unique($rows, SORT_REGULAR) as $row) {
        $result[$row['DayofWeek']][] = $row;
    }
    

    你可以运行代码here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-19
      • 2012-02-11
      • 2017-11-01
      • 1970-01-01
      • 2014-10-30
      • 1970-01-01
      • 2019-10-05
      相关资源
      最近更新 更多