3

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[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';
1
  • 1
    try to reorganize you array, it's not very convenient to display it the way you want... Commented Jun 29, 2011 at 7:20

4 Answers 4

2

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 />';
   }
}
Sign up to request clarification or add additional context in comments.

2 Comments

That's inefficient. You dont need three foreach'es to achieve the desired output, nor do you need to restructure the array.
@Gordon I know. If there's an order on company, you should only need one foreach. Either way, this is the most robust way I know of given the information in the question, without making the assumptions you have.
0

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 />';
    }
}

Comments

0

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.

Comments

0

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'];
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.