How can I display the contents of an array as follows:
Company Name
- Username1
- Username2
Another Company Name
- Username3
The array I have created is as follows:
$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开发者_C百科[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';
This should do it:
$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 />';
}
}
Just one approach. This restructures the array to associate users by company, then just iterates through them.
$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 />';
}
}
first you need to get them organized by company:
$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.
}
Then you can just loop through and output.
This should do it with the existing structure in one iteration (demo):
$currentCompanyId = 0;
foreach ($array as $company) {
if ($currentCompanyId !== $company['company_id']) {
printf("%s\n", $company['company_name']);
$currentCompanyId = $company['company_id'];
}
printf("\t-%s\n", $company['user_name']);
}
Output:
ABC Company
-Andrew
-Jeffrey
Some Company
-William
My Company
-Jaime
The snippet assumes the array is sorted by Company Name (like shown in your code example). If that isnt the case, sort it by Company ID first:
usort($array, function($companyA, $companyB) {
return $companyA['company_id'] - $companyB['company_id'];
});
精彩评论