我有一个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']);
}
这是可行的,但它没有检查如果(!在“数组”的右边,它将每个(重复的)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:在尝试添加到day数组之前,如何让它检查索引[sessionid]?