1

I have tried multiple solutions that I have found around and none seem to be working. Can anyone give me any help? I want the result to return the ID's ($user) and currently it returns [object Object] with the code below. $users gives me all the sites user ID's and $pageview gets the number of times the page has been visited. If I change the return statement to $test I get all the values from $pageview correclty as they should be. The need for adding the key 'count' as $pageview is so that I can sort the array by the value of pageview in order to order the values based on the most popular page. I know this isn't the most efficient way to do this and using a proper analytics system would be better but for this task I need a method like this.

 if($args == 'Popular'){ $users = get_all_user_ids(''); foreach($users as $user) { $pageview = get_user_meta($user, 'page_visits', true); $test[] = $pageview; if ($pageview != 0) { $popularid[$pageview]['count'] = $user; } else { continue; } rsort($popularid); } return $popularid; } 

The output is being encoded with json_encode after it has gone through this if statement and is then output onto the page in a JS function using document.getElementById("cm-output").innerHtml.

Thanks in advance.

5
  • What's the function of this line: $pageviews = $pageview + $user; Commented Sep 24, 2018 at 11:29
  • If you're getting an object have you tried var dumping it? Commented Sep 24, 2018 at 11:44
  • @TamilvananN That line adds the values together so that the if statement can check if anything has been added to the user ID, I realise I could just check if its equal to 0. Commented Sep 24, 2018 at 12:35
  • @Aravona I have tried var dumping it and it doesn't return anything. I should probably add that the information is being json_encoded after it gets the ID's and then is being output onto the page with .innerHtml Commented Sep 24, 2018 at 12:36
  • @M.F. Yess... You now checked based on 0. I think array str with key needs to be changed. Check the updated answer. Commented Sep 24, 2018 at 13:05

1 Answer 1

0

Let's try the below code,

$users = get_all_user_ids(''); $i=0; // initialization foreach($users as $user) { $pageview = get_user_meta($user, 'page_visits', true); if ($pageview != 0) { //$popularid[$pageview]['count'] = $user; // Err: // Array won't be unique due to pageview might be same for multiple users // $popularid[$user] = $pageview; // Array will be unique based on user id $popularid[$i]['user'] = $user; $popularid[$i]['pageview'] = $pageview; $i++; // incrementor } } //rsort($popularid); // Sorts based on value usort($popularid, function($a, $b) { return $a['pageview'] - $b['pageview']; }); $userArr = array_column($popularid, 'user'); //return $popularid; return $userArr; 

Update: Core PHP code example for usort

$array[] = array('pageview'=>4,'user'=>'abc'); $array[] = array('pageview'=>2,'user'=>'xyz'); $array[] = array('pageview'=>1,'user'=>'pqrs'); $array[] = array('pageview'=>3,'user'=>'ijk'); usort($array, function($a, $b) { return $a['pageview'] - $b['pageview']; }); $userArr = array_column($array, 'user'); // to get only the values from user key print_r($userArr); 
20
  • That does give me the results in the correct order which is the first step but I need the output to be the ID's set in $user rather than the values themselves in $pageview as I pass these ID's into another function to get the user avatars with the user ID's. Commented Sep 24, 2018 at 13:11
  • Do you want hight to low order? And it's based on page view(s) correct?? Commented Sep 24, 2018 at 13:15
  • Correct, I need the highest value first and the lowest value last based on the value of $pageview but outputting the value of $user. Commented Sep 24, 2018 at 13:17
  • So, In the return array you don't want the page views. Only user id's in the desired order. Commented Sep 24, 2018 at 13:22
  • 1
    Thats the one! Thank you so much, I appreciate your patience in figuring it out with me. Commented Oct 12, 2018 at 9:13

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.