-1

I'm trying to loop through all the pages of a determined forum thread and get the total number of posts by each user but I'm struggling to understand how to sum each time the username is repeated inside the array.

Here's my code:

for ($x = 1; $x <= 5; $x++) {

    $ch = curl_init('http://myforum.com/thread-123?&page=' . $x);

    curl_setopt_array($ch, array(
        CURLOPT_HEADER => TRUE,
        CURLOPT_RETURNTRANSFER => TRUE
    ));

    $response = curl_exec($ch);

    preg_match_all('/me\">[\s]+<a href=\"([^\"]+)\">([^<]+)/i', $response, $users);
    preg_match_all('/image_avatar\" src=\"([^\"]+)"/i', $response, $avatar);


    $q = count($users[1]);

    for ($i = 0; $i < $q; $i++) {
        ///////count how many times the user was inside the array///////
        //$totalPosts = ?
        ///////count how many times the user was inside the array///////

        print '-----<br /><img src="'.$avatar[1][$i].'" /><br />User: ' . $users[2][$i] . '<br />Total Posts: ' . $totalPosts . '-----<br />';        

    }


}

Solution, thanks @u_mulder

$all_users = array();
for ($x = 1; $x <= 5; $x++) {

    $ch = curl_init('http://myforum.com/thread-123?&page=' . $x);

    curl_setopt_array($ch, array(
        CURLOPT_HEADER => TRUE,
        CURLOPT_RETURNTRANSFER => TRUE
    ));

    $response = curl_exec($ch);

    preg_match_all('/me\">[\s]+<a href=\"([^\"]+)\">([^<]+)/i', $response, $users);
    preg_match_all('/image_avatar\" src=\"([^\"]+)"/i', $response, $avatar);


    $all_users = array_merge($all_users, $users[2]);


}
function aprint($arr, $return = false) {
    arsort($arr);
    $wrap = '<div style=" white-space:pre; position:absolute; top:10px; left:10px; height:200px; width:100px; overflow:auto; z-index:5000;">';
    $wrap = '<pre>';
    $txt = preg_replace('/(\[.+\])\s+=>\s+Array\s+\(/msiU','$1 => Array (', print_r($arr,true));

    if ($return) return  $wrap.$txt.'</pre>';
    else echo $wrap.$txt.'</pre>';
}

$res = array_icount_values ($all_users);
$res2 = aprint($res);
print_r($res2); 

function array_icount_values($arr,$lower=true) { 
     $arr2=array(); 
     if(!is_array($arr['0'])){$arr=array($arr);} 
     foreach($arr as $k=> $v){ 
      foreach($v as $v2){ 
      if($lower==true) {$v2=strtolower($v2);} 
      if(!isset($arr2[$v2])){ 
          $arr2[$v2]=1; 
      }else{ 
           $arr2[$v2]++; 
           } 
    } 
    } 
    return $arr2; 
}

Example output (Username => Total posts) also ordered by total posts:

Array
(
    [user 1] => 35
    [user 2] => 11
    [user 3] => 11
    [user 4] => 8
    [user 5] => 5
    [user 6] => 4
    [user 7] => 3
    [user 8] => 2
    [user 9] => 2
    [user 10] => 2
    [user 11] => 2
    [user 12] => 1
    [user 13] => 1
    [user 14] => 1
    [user 15] => 1
    [user 16] => 1
    [user 17] => 1
    [user 18] => 1
    [user 19] => 1
    [user 20] => 1
    [user 21] => 1
    [user 22] => 1
    [user 23] => 1
    [user 24] => 1
    [user 25] => 1
    [user 26] => 1
)
Hizan
  • 35
  • 5
  • It would help, if you please add some sample `$responce` as well. – Ravi Dhoriya ツ Mar 13 '14 at 06:39
  • Sorry, you will have to add more details: it is unclear what arrays you use, what structure they have and where that user name might be found in there. – arkascha Mar 13 '14 at 06:39
  • Is this for more than one page? Or is it just a forum thread? If you want to get a total for the entire forum you may want to have a counter on the database otherwise here is a link that will help you out. http://stackoverflow.com/questions/13633954/how-do-i-count-occurrence-of-duplicate-items-in-array – Chitowns24 Mar 13 '14 at 06:44
  • I'll try to make a sample. And the array structure would be like this: `$users = array('user found 1', 'user found 2', 'user found 3', 'user found 1', 'user found 2', 'user found 1', 'user found 1');` you can see that "user found 1" posted more than 2 times etc, I would like to print something like username and times posted – Hizan Mar 13 '14 at 06:48
  • @Chitowns24 I don't have access to the database. And yes it's more than one page, the loop goes to page 1, add all found usernames to the array, then page 2, same thing, etc. – Hizan Mar 13 '14 at 06:50

1 Answers1

1

For counting frequency of items in array you can use array_count_values function.

Code for array you provided as example:

$users = array('user found 1', 'user found 2', 'user found 3', 'user found 1', 'user found 2', 'user found 1', 'user found 1');
print_r(array_count_values($users));

Result is:

Array ( [user found 1] => 4 [user found 2] => 2 [user found 3] => 1 ) 

Update Ok, if you have different pages then try something like this:

$all_users = array();
for ($x = 1; $x <= 5; $x++) {
    // ....
    preg_match_all('/me\">[\s]+<a href=\"([^\"]+)\">([^<]+)/i', $response, $users);

    $all_users = array_merge($all_users, $users[1]);
}
print_r(array_count_values($all_users));
u_mulder
  • 54,101
  • 5
  • 48
  • 64
  • But I don't know the values of the $users array, each page the loop goes through has a different order and variety of users. `print_r(array_count_values($users);` prints `Array ( )` – Hizan Mar 13 '14 at 07:06
  • If you don't know the values of `$users` array then what are you going to count? – u_mulder Mar 13 '14 at 07:10
  • See http://pastebin.com/Fn7F6CHD, each page the loop goes through has the same layout but different usernames each time, I want to print all the usernames and how many times they are duplicated inside the `$users` array – Hizan Mar 13 '14 at 07:13
  • I swear I found a solution before seeing your answer (: . Anyways, I did something similar to what you did but instead of using array_merge I just used array_push inside my `for ($i = 0; $i < $q; $i++)` and added some formatting stuff, is there any benefit from array_merge over push in this case? Well I'll update my OP soon with your solution plus the formatting stuff I did. Thank you very much! – Hizan Mar 13 '14 at 07:59