我创建了连接四个表的db查询,并在codeigniter中的数组中返回以下结果。
Array
(
[results] => Array
(
[0] => stdClass Object
(
[slip_id] => 257
[slip_num] => 32
[project_no] => SWS153
[client_id] => 1
[dwg_id] => 1
[user_id] => 1
[issue_date] => 2015-10-25 15:00:32.000000
[sws_dwg_no] => SWS153-100
[client_dwg_no] =>
[dwg_title] => Pipework Suction and Discharge: General Arrangement
[dwg_by] =>
[dwg_rev] => D
[dwg_date] => 2015-10-25 15:00:32
[rev_id] => 143
[clientName] => R
[clientSurname] => C
[clientCom] => Pumps and Valves (Pty.) Ltd.
[id] => 1
[email] => admin@domain.com
[password] => 5f4dcc3b5aa765d61d8327deb882cf99
[name] => Admin
[lastname] => Admin
[project_name] => Metalong Raw Water Pump Station
)
[1] => stdClass Object
(
[slip_id] => 258
[slip_num] => 32
[project_no] => SWS153
[client_id] => 1
[dwg_id] => 2
[user_id] => 1
[issue_date] => 2015-10-25 15:00:32.000000
[sws_dwg_no] => SWS153-107
[client_dwg_no] =>
[dwg_title] => Pipework isolation valve chamber & surg vessel
[dwg_by] =>
[dwg_rev] => D
[dwg_date] => 2015-10-25 15:00:32
[rev_id] => 144
[clientName] => R
[clientSurname] => C
[id] => 1
[email] => admin@domain.com
[password] => 5f4dcc3b5aa765d61d8327deb882cf99
[name] => Admin
[lastname] => Admin
[project_name] => Metalong Raw Water Pump Station
)
[2] => stdClass Object
(
[slip_id] => 259
[slip_num] => 32
[project_no] => SWS153
[client_id] => 1
[dwg_id] => 3
[user_id] => 1
[issue_date] => 2015-10-25 15:00:32.000000
[sws_dwg_no] => SWS153-112
[client_dwg_no] =>
[dwg_title] => Pipework discharge pipe pieces plan and details
[dwg_by] =>
[dwg_rev] => D
[dwg_date] => 2015-10-25 15:00:32
[rev_id] => 145
[clientName] => R
[clientSurname] => C
[id] => 1
[email] => admin@domain.com
[password] => 5f4dcc3b5aa765d61d8327deb882cf99
[name] => Admin
[project_name] => Metalong Raw Water Pump Station
)
[3] => stdClass Object
(
[slip_id] => 260
[slip_num] => 32
[project_no] => SWS153
[client_id] => 1
[dwg_id] => 4
[user_id] => 1
[issue_date] => 2015-10-25 15:00:32.000000
[sws_dwg_no] => SWS153-113
[client_dwg_no] =>
[dwg_title] => Pipework suction and discharge setting out layout
[dwg_by] =>
[dwg_rev] => D
[dwg_date] => 2015-10-25 15:00:32
[rev_id] => 146
[clientName] => R
[clientSurname] => C
[id] => 1
[email] => admin@domain.com
[password] => 5f4dcc3b5aa765d61d8327deb882cf99
[name] => Admin
[project_name] => Metalong Raw Water Pump Station
)
[4] => stdClass Object
(
[slip_id] => 261
[slip_num] => 32
[project_no] => SWS153
[client_id] => 3
[dwg_id] => 1
[user_id] => 1
[issue_date] => 2015-10-25 15:00:32.000000
[sws_dwg_no] => SWS153-100
[client_dwg_no] =>
[dwg_title] => Pipework Suction and Discharge: General Arrangement
[dwg_by] =>
[dwg_rev] => D
[dwg_date] => 2015-10-25 15:00:32
[rev_id] => 143
[clientName] => F
[clientSurname] => N
[id] => 1
[email] => admin@domain.com
[password] => 5f4dcc3b5aa765d61d8327deb882cf99
[name] => Admin
[project_name] => Metalong Raw Water Pump Station
)
[5] => stdClass Object
(
[slip_id] => 262
[slip_num] => 32
[project_no] => SWS153
[client_id] => 3
[dwg_id] => 2
[user_id] => 1
[issue_date] => 2015-10-25 15:00:32.000000
[sws_dwg_no] => SWS153-107
[client_dwg_no] =>
[dwg_title] => Pipework isolation valve chamber & surge vessel
[dwg_by] =>
[dwg_rev] => D
[dwg_date] => 2015-10-25 15:00:32
[rev_id] => 144
[clientName] => F
[clientSurname] => N
[id] => 1
[email] => admin@domain.com
[password] => 5f4dcc3b5aa765d61d8327deb882cf99
[name] => Admin
[project_name] => Metalong Raw Water Pump Station
)
[6] => stdClass Object
(
[slip_id] => 263
[slip_num] => 32
[project_no] => SWS153
[client_id] => 3
[dwg_id] => 3
[user_id] => 1
[issue_date] => 2015-10-25 15:00:32.000000
[sws_dwg_no] => SWS153-112
[client_dwg_no] =>
[dwg_title] => Pipework discharge pipe pieces plan and details
[dwg_by] =>
[dwg_rev] => D
[dwg_date] => 2015-10-25 15:00:32
[rev_id] => 145
[clientName] => F
[clientSurname] => N
[id] => 1
[email] => admin@domain.com
[password] => 5f4dcc3b5aa765d61d8327deb882cf99
[name] => Admin
[project_name] => Metalong Raw Water Pump Station
)
[7] => stdClass Object
(
[slip_id] => 264
[slip_num] => 32
[project_no] => SWS153
[client_id] => 3
[dwg_id] => 4
[user_id] => 1
[issue_date] => 2015-10-25 15:00:32.000000
[sws_dwg_no] => SWS153-113
[client_dwg_no] =>
[dwg_title] => Pipework suction and discharge setting out layout
[dwg_by] =>
[dwg_rev] => D
[dwg_date] => 2015-10-25 15:00:32
[rev_id] => 146
[clientName] => F
[clientSurname] => N
[id] => 1
[email] => admin@domain.com
[password] => 5f4dcc3b5aa765d61d8327deb882cf99
[name] => Admin
[project_name] => Metalong Raw Water Pump Station
)
)
)
我想使用client_id创建关联数组,因此我可以将客户端的信息分组并一起显示。这是为了生成收据。可以将相同的文档发送到不同的客户端,因此必须按客户端显示收据。
我试图创建一个foreach循环来重新组织数组。
答案 0 :(得分:1)
这会将结果分组到一个数组中,原始数组称为$ arr:
$result = array();
foreach( $arr['results'] as $v ) {
$result[$v->client_id][] = $v;
}
要获得单个客户端($ client_id),所有结果都会像这样显示
foreach( $result[$client_id] as $res ) {
// Use the data
echo 'Slip: ' . $res->slip_id . "<br />\n"
. 'Title: ' . $res->dwg_title . "<br />\n";
}
要显示客户订购的所有内容,请执行以下操作:
foreach( $result as $client_id => $temp ) {
echo 'Client_id: ' . $client_id . "<br />\n";
foreach( $temp as $res ) {
// Use the data
echo 'Slip: ' . $res->slip_id . "<br />\n"
. 'Title: ' . $res->dwg_title . "<br />\n";
}
}
这是你问题的答案吗?
Edit2 $res
是一个对象...
显示单个客户端的方法和显示所有客户端的方法!