0

I'm having trouble printing the results of this array in loop (to be displayed on the front end). The objective to be able to get the chapter name (eg, Chapter_Name_Unique), that chapter name's total chapter post views count, and then the fullname of each chapter_member within that chapter name (or group). I'm thinking that something isn't structured properly here, because I'm having issues looping through.

Is my logic off?

print_r looks like this:

Array
(
 [Chapter_Name_Unique] => Array
    (
        [chapter_post_views_count] => 3338
        [chapter_members] => Array
            (
                [0] => Array
                    (
                        [post_views_count] => 3338
                        [first_name] => Mary
                        [last_name] => Jane
                        [fullname] => maryjane
                        [chapter_name] => Chapter_Name_Unique
                    )

            )

    )

[Chapter_Name_Unique_2] => Array
    (
        [chapter_post_views_count] => 783
        [chapter_members] => Array
            (
                [0] => Array
                    (                    
                        [post_views_count] => 404
                        [first_name] => Betty
                        [last_name] => Lou
                        [fullname] => bettylou
                        [chapter_name] => Chapter_Name_Unique_2
                    )

                [1] => Array
                    (
                        [post_views_count] => 379
                        [first_name] => Judy
                        [last_name] => Jones
                        [fullname] => judyjones
                        [chapter_name] => Chapter_Name_Unique_2
                    )
            )
    )
)

and the actual functions look like this:

$grouped_types = array();

// to add together post counts of group members
foreach($users as $chapter){ 
$grouped_types[$chapter['chapter_name']]['chapter_post_views_count'] += $chapter['post_views_count'];
}

// to group on top level by chapter_name
foreach($users as $chapter){ 
$grouped_types[$chapter['chapter_name']]['chapter_members'][] = $chapter;
}

echo "<pre>";
print_r( $grouped_types );
echo "</pre>";
Caroline
  • 104
  • 1
  • 10
  • 1
    Looking at your array output you should be able to get those two values with: `foreach($grouped_types as $chapter_name_unique => $vars) { $chapter_post_views = $vars['chapter_post_views_count']; }` – scrowler Aug 11 '14 at 01:52
  • Question - if I wanted to sort this by [chapter_post_views_count] - what is the best method/time to do this, before or after the foreach loop (the final one commented below)? – Caroline Aug 11 '14 at 13:14
  • 1
    [Have a look at this question](http://stackoverflow.com/questions/17364127/reference-all-basic-ways-to-sort-arrays-and-data-in-php) – scrowler Aug 11 '14 at 20:48

2 Answers2

1

Try something like (assuming that $users is your array from your print_r())

$grouped_types = array();

foreach($users as $chapter=>$data){ 

    $grouped_types[$chapter]['chapter_post_views_count'] = $data['chapter_post_views_count'];

    foreach($data['chapter_members'] as $member){ 

        $grouped_types[$chapter]['chapter_members'][] = $members['fullname'];

    }

}

echo "<pre>";
print_r( $grouped_types );
echo "</pre>";
Sean
  • 12,443
  • 3
  • 29
  • 47
-1

Thanks so much guys, your tips really helped. I wound up using a bit from both, and pulling this together:

foreach( $grouped_types as $chapter_name_unique => $vars) { 
$chapter_post_views = $vars['chapter_post_views_count'];
$chapter_members = $vars['chapter_members']; 

echo $chapter_name_unique; // echo chapter name (level 1)
echo $chapter_post_views; // echo chapter post views (level 2 - $vars before)

foreach( $vars['chapter_members'] as $member){ // loop through all chapter_members

    $vars['chapter_members'][] = $member['fullname'];
    echo $member['fullname']; // echo all usernames within the group

    }
}

In combination with the two other foreach statements above (they resort and add things together saving to the $grouped_types array), this works exactly as I needed it to. (Hopefully not too much unnecessary looping?)

Thanks again!

Caroline
  • 104
  • 1
  • 10