How to get a value from an Array after I use array_count_values() function

I am trying to get the top 10 most displayed keywords in the array.
Storing the keyword into an array
 
while($row = mssql_fetch_array($rs)){ 
     $keyWords[] = $row["MetaKeywords"];
}
		
 
print_r($keyWords);		
$newArr = array();
foreach($keyWords as $val)
{
  $itemParts = split(',', $val);
        foreach($itemParts as $item)
        array_push($newArr, trim($item));
}
print "<pre>";
print_r($newArr);
print "</pre>";
 
$arCount = array_count_values($newArr);
print "<pre>";
print_r($arCount);
print "</pre>";
 
//sizeof($arCount); 
 $i = 0;
while($i <= 10 ){
	echo $arCount[0];
	$i++;
}
 
I get these results...
 
 results of array 1
---------------------
    [0] => football games
    [1] => 2008 season
    [2] => alochol
    [3] => archery park
    [4] => Becky and Bob Humpries
    [5] => John and Marnie Demmer
    [6] => shooting
    [7] => sports
    [8] => education
    [9] => training center
    [10] => sports
    [10] => sports
etc..
 
results of array 2
---------------------
    [0] => 3
    [1] => 3
    [2] => 2
    [3] => 2
    [4] => 2
    [5] => 2
    [6] => 2
    [7] => 2
    [8] => 2
)
 
results of loop
----------------
33333333333
 
Is there any way to get the TOP values of the $arCount array such as it should print like
$arCount[0] = "Sports";
 
Thanks.

Open in new window

martyjeAsked:
Who is Participating?
 
Richard DavisConnect With a Mentor Senior Web DeveloperCommented:
OKAY!!!...PHEW!!!

Try this on for size! LOL :)

~A~
<?php
// this is just to replicate a data source that matches what you described
// I'll assume that these values are actually database records
$keyWords[0] = "football games";
$keyWords[1] = "shooting";
$keyWords[2] = "alochol";
$keyWords[3] = "archery park";
$keyWords[4] = "Becky and Bob Humpries";
$keyWords[5] = "John and Marnie Demmer";
$keyWords[6] = "shooting";
$keyWords[7] = "sports";
$keyWords[8] = "education";
$keyWords[9] = "training center";
$keyWords[10] = "sports";
$keyWords[11] = "sports";
$keyWords[12] = "sports";
$keyWords[13] = "training center";
$keyWords[14] = "alochol";
$keyWords[15] = "archery park";
$keyWords[16] = "Becky and Bob Humpries";
$keyWords[17] = "John and Marnie Demmer";
$keyWords[18] = "shooting";
$keyWords[19] = "sports";
$keyWords[20] = "sports";
 
$arCount = array_count_values($keyWords);
print "<pre>";
print 'arCount<br />';
print_r($arCount);
print "</pre>";
 
$i = 0;
foreach($arCount as $key => $val)
{
  $tmpArr[$i] = $key;
  $i++;
}
 
print "<pre>";
print 'tmpArr<br />';
print_r($tmpArr);
print "</pre>";
 
// Comparison function
function cmp($a, $b)
{
  if($a == $b) return 0;
 
  return ($a < $b) ? -1 : 1;
}
 
uasort($arCount, 'cmp');
$arSorted = array_reverse($arCount);
print "<pre>";
print 'arCount sorted<br />';
print_r($arSorted);
print "</pre>";
 
foreach($arSorted as $key => $val)
{
  echo $key.'<br />';
}
?>

Open in new window

0
 
PhatzerCommented:
Try replacing:

 $i = 0;
while($i <= 10 ){
        echo $arCount[0];
        $i++;
}

With:

$i = 0;
while($i <= 10){
        echo $arCount[$i];
        $i++;
}
0
 
martyjeAuthor Commented:
Sorry mistypo, was just checking if there's something in the first value of array. ... it's always 3.
I changed it to $i, still shows 3 not the value of the array.

0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
PhatzerCommented:
Is it using the correct array?:

$i = 0;
while($i <= 10){
        echo $newArr[$i];
        $i++;
}
0
 
PhatzerCommented:
I see what you're doing now, sorry. OK. I'm wondering if it's simply the key name you need.

Give this a quick try, it might just return numbers (it shouldn't, but it might!)...

$i = 0;
while($i <= 10){
        echo key($arCount[$i]);
        $i++;
}
0
 
martyjeAuthor Commented:
Yea that gives an error
Warning: key() [function.key]: Passed variable is not an array or object in ...

0
 
Richard DavisSenior Web DeveloperCommented:
Just change key to $key, martyje.
0
 
Richard DavisSenior Web DeveloperCommented:
Ooops!
My bad.

You're getting that error because $arCount[$i] is not an array. It's an array element and key() is expecting an array.

key($arCount)
0
 
martyjeAuthor Commented:
So how do I get the values?
$i = 0;
while($i <= 10 ){
      echo key($arCount);
      $i++;
}

This shows ten zeros.
Thanks.
0
 
Richard DavisSenior Web DeveloperCommented:
Give me a few moments...I'm actually writing you up the code right now, martyje.
0
 
Richard DavisSenior Web DeveloperCommented:
I haven't abandon your question martyje...just having one HECK of a time with hacking it out...not to mention a girlfriend that won't leave me alone long enough to stay mentally on track...GRRR!!!

Still hashing this out for you though.
0
 
psadacCommented:
use array functions, it's easier and faster :
while($row = mssql_fetch_array($rs)){
     $keyWords[] = $row["MetaKeywords"];
}
 
 
print_r($keyWords);
$newArr = array();
foreach($keyWords as $val)
{
    $itemParts = split(',', $val);
    foreach($itemParts as $item) {
        if (isset($newArr[$item]) {
            $newArr[$item]++;
        } else {
            $newArr[$item] = 0;
        }
    }
}
arsort($newArr);
 
print "<pre>";
print_r(array_slice($newArr, 0, 10));
print "</pre>";

Open in new window

0
 
martyjeAuthor Commented:
adrian_brooks: I know how you feel man. Good luck with that.
psadac: the algorithm doesn't seems to be working, it didn't display the keywords with count value of 3 and skipped some that are in the list twice.

Thanks.

0
 
psadacCommented:
"adrian_brooks: I know how you feel man. Good luck with that."
my solution does not work so you should not feel sorry for adrian ;-)
however this kind of situation happened several times to me, and it was probably the most interesting times on EE : you can learn from other people how to do things in a way you never thought. if you want to reward adrian, just do it, i don't really care for the points, what matters is the challenge.

back to the problem, could you post the results, i don't really see what's wrong with my algorithm ?
0
 
martyjeAuthor Commented:
adrian_brooks: Works perfect, just one little thing. In the "arCount Sorted" array. It shows zero's and 1's and 2's. (last value in the array bellow. In the actual keywords, I don't have zeros or anything, I printed the output just to make sure and didn't find any.
Any idea why it's there?

Thanks much.

arCount sorted
Array
(
    [sports] => 3
    [China] => 3
    [Duffy Dundel Football Building] => 2
    [Keystone football] => 2
    [plaza] => 2
    [naming] => 2
    [Ferari Football Center] => 2
    [game] => 2
    [Mark Jenethon] => 2
    [0] => 2
0
 
Richard DavisSenior Web DeveloperCommented:
I would have to defer to saying that there must be something erroneous in your original data (keyWords).

I was able to replicate that issue by adding $keyWords[21] = 0; to my original data array.

~A~
0
 
martyjeAuthor Commented:
Thanks much. You Da man.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.