如何按如下方式显示数组的内容:
Company Name
- Username1
- Username2
Another Company Name
- Username3
我创建的阵列如下:
$array[1]['company_id'] = '12';
$array[1]['company_name'] = 'ABC Company';
$array[1]['company_type'] = 'default';
$array[1]['user_id'] = '23';
$array[1]['user_name'] = 'Andrew';
$array[2]['company_id'] = '12';
$array[2]['company_name'] = 'ABC Company';
$array[2]['company_type'] = 'default';
$array[2]['user_id'] = '27';
$array[2]['user_name'] = 'Jeffrey';
$array[3]['company_id'] = '1';
$array[3]['company_name'] = 'Some Company';
$array[3]['company_type'] = 'default';
$array[3]['user_id'] = '29';
$array[3]['user_name'] = 'William';
$array[4]['company_id'] = '51';
$array[4]['company_name'] = 'My Company';
$array[4]['company_type'] = 'default';
$array[4]['user_id'] = '20';
$array[4]['user_name'] = 'Jaime';
这应该做到:
$array[1]['company_id'] = '12';
$array[1]['company_name'] = 'ABC Company';
$array[1]['company_type'] = 'default';
$array[1]['user_id'] = '23';
$array[1]['user_name'] = 'Andrew';
$array[2]['company_id'] = '12';
$array[2]['company_name'] = 'ABC Company';
$array[2]['company_type'] = 'default';
$array[2]['user_id'] = '27';
$array[2]['user_name'] = 'Jeffrey';
$array[3]['company_id'] = '1';
$array[3]['company_name'] = 'Some Company';
$array[3]['company_type'] = 'default';
$array[3]['user_id'] = '29';
$array[3]['user_name'] = 'William';
$array[4]['company_id'] = '51';
$array[4]['company_name'] = 'My Company';
$array[4]['company_type'] = 'default';
$array[4]['user_id'] = '20';
$array[4]['user_name'] = 'Jaime';
foreach( $array as $company ) {
$companies[$company['company_name']]['users'][] = $company['user_name'];
}
echo '<pre>' . print_r( $companies, true ) . '</pre>';
foreach( $companies as $companyname => $company ) {
echo $companyname . '<br />';
foreach( $company['users'] as $user ) {
echo ' - ' . $user . '<br />';
}
}
只有一种方法。这将重组数组,使其按公司关联用户,然后只遍历用户。
$companies = array();
foreach($array as $user)
{
$companies[$user['company_name']][] = $user['user_name'];
}
foreach($companies as $name => $userlist)
{
echo $name . '<br />';
foreach($userlist as $user)
{
echo ' - ' . $user . '<br />';
}
}
首先你需要让他们按公司组织:
$companies = array();
foreach($array as $i => $user)
{
$id = $user['company_id'];
if(!isset($companies[$id])){
$companies[$id] = array(
'company_name' => $user['company_name'],
'company_type' => $user['company_type'],
'company_id' => $id,
'users' => array();
);
}
$companies[$id]['users'][] = array(
'user_name' => $user['user_name'],
'user_id' => $user['user_id']
);
unset($array[$i]); // just remove the element from the source array... not necessary but possibly useful if its a HUGE list.
}
然后你就可以循环并输出了。
这应该在一次迭代(演示)中使用现有结构:
$currentCompanyId = 0;
foreach ($array as $company) {
if ($currentCompanyId !== $company['company_id']) {
printf("%sn", $company['company_name']);
$currentCompanyId = $company['company_id'];
}
printf("t-%sn", $company['user_name']);
}
输出:
ABC Company
-Andrew
-Jeffrey
Some Company
-William
My Company
-Jaime
该代码段假定数组按公司名称排序(如代码示例所示)。如果不是这样,请先按公司ID进行排序:
usort($array, function($companyA, $companyB) {
return $companyA['company_id'] - $companyB['company_id'];
});