You are not logged in.

#1 2006-12-09 04:55:18

jb
Member
From: Florida
Registered: 2006-06-22
Posts: 466

[Solved][PHP] Generating & sorting multidimensional arra

I'm trying to make a function that takes an array of tags, counts the number each occurs, and returns an array with the most common descending.

I have 1½ problems:
1.) when I use the function, it keeps saying "notice: undefined index: counts" at the line I note in the code
½.) I get the feeling there is a far cleaner way to do this that I'm just not aware of.

So far, I'm not using $no2return yet, and I'm aware it's still returning all the columns of the array:

    function GetTopTags($no2return)
    {
        $tags = array("gcc","bash","perl","gcc","asm","recursion",
          "fileIO","bash","solaris","gcc","perl","asm","haiku","haiku");
        $i = 14;
        // Sort array to make assumptions on generating stats
        // checks if current tag is not tag in the stats and
        // adds the new one. It then increments the count column.
        sort($tags);
        $k = 0;
        $tagStats[0]["tags"] = $tags[0];
        $tagStats[0]["counts"] = 1;
        for($j = 1; $j < $i; $j++)
        {
            if(!($tags[$j] == $tagStats[$k]["tags"]))
            {
                $k++;
                $tagStats[$k]["tags"] = $tags[$j];
            }
            $tagStats[$k]["counts"] += 1; //<=Claims undefined index here.
        }

        $returnTags = columnSort($tagStats,"counts");

        return $returnTags;
    }

function columnSort($unsorted, $column) {
    $sorted = $unsorted;
    for ($i=0; $i < sizeof($sorted)-1; $i++) {
      for ($j=0; $j<sizeof($sorted)-1-$i; $j++)
        if ($sorted[$j][$column] < $sorted[$j+1][$column]) {
          $tmp = $sorted[$j];
          $sorted[$j] = $sorted[$j+1];
          $sorted[$j+1] = $tmp;
      }
    }
    return $sorted;
 }

Any help on this would be greatly appreciated. 
Thanks.


...

Offline

#2 2006-12-09 06:42:35

noriko
Member
From: In My Mind
Registered: 2006-06-09
Posts: 535
Website

Re: [Solved][PHP] Generating & sorting multidimensional arra

edit#:truncated


The.Revolution.Is.Coming - - To fight, To hunger, To Resist!

Offline

#3 2006-12-09 08:14:43

noriko
Member
From: In My Mind
Registered: 2006-06-09
Posts: 535
Website

Re: [Solved][PHP] Generating & sorting multidimensional arra

<?
error_reporting(E_ALL);
   function GetTopTags($no2return)
   {
      $tags = array("gcc","bash","perl","gcc","asm","recursion",
          "fileIO","bash","solaris","gcc","perl","asm","haiku","haiku");
      $i = 14;
      // Sort array to make assumptions on generating stats
      // checks if current tag is not tag in the stats and
      // adds the new one. It then increments the count column.
      sort($tags);
      $k = 0;
      $tagStats[0]["tags"] = $tags[0];
      $tagStats[0]["counts"] = 1;
      for($j = 1; $j < $i; $j++)
      {
         if($tags[$j] != $tagStats[$k]["tags"])
         {
            
            $tagStats[++$k]["tags"] = $tags[$j];

         }

      #$tagStats[$k]["counts"] = 1;   
#echo $tagStats[$k]["counts"] += 1;  //<=Claims undefined index here
/*i dunno if i misread this, but all $tagStats[$k]["counts"]`s will be equal;
therefore it's better to just go ahead and assign the value 1/0
i'm  fairly sure that's not what ur doing though... 
i'm confused lol


correct me if i'm wrong
but were u intending to incrementense to  $tagStats[0]["counts"] ? ; 
that'd make more sense here IMO */ 

#or $tagStats[$k]["counts"] = $tagStats[0]['counts']++;      
# or $tagStats[$k]['counts'] = 1; //this would get rid of the indexing error,
## i dunno what ur trying to do lol... i'm noob...
}

      $returnTags = columnSort($tagStats,"counts");

      return $returnTags;
   }

function columnSort($sorted, $column) {
    
$si = count($sorted);
    for ($i=1; $i < $si; $i++) {
      for ($j=0; $j < $si-$i; $j++)
        if ($sorted[$j][$column] <sorted>


i just managed to test it a lil;
i changed a few things ;; it's all about tastes imo;;
i changed the loops... started at 1 instead of 0, this saves u having to subtract 1 everytime...and i created a var $si; with the size of the array; saves u having to recount the array all the time....

ignore my first post, if u do that, u will get all $k[ being 2 ...


The.Revolution.Is.Coming - - To fight, To hunger, To Resist!

Offline

#4 2006-12-10 06:39:46

jb
Member
From: Florida
Registered: 2006-06-22
Posts: 466

Re: [Solved][PHP] Generating & sorting multidimensional arra

Just fixed my notice.  Probably would have been helpful if I'd stopped to take a moment and ask myself why it claimed undefined.

      for($j = 1; $j < $i; $j++) 
       { 
          if(!($tags[$j] == $tagStats[$k]["tags"])) 
          { 
             $k++; 
             $tagStats[$k]["tags"] = $tags[$j]; 
             $tagStats[$k]["counts"] = 0; //<= This is what I forgot, now it all works, still messy code though.
          } 
          $tagStats[$k]["counts"] += 1; //<=Claims undefined index here. 
       } 

Alright, here's what I'm going for; I'm trying to keep track of how many of each tags there are.  I can't make any assumption as to what the tags are.  Since the array is sorted, I can assume that when $tags[$j] stops equalling $tagStats[$k]["tags"], there's no more $tagStats[$k]["tags"] and I put the next tag into the stats.  Regardless if it's a tag or not, I just increment it to signify that the tag occured that instance.

Thanks for posting though, when I saw your

$tagStats[0]["tags"] = $tags[0]; 
$tagStats[0]["counts"] = 1; 

I realized what happened. I won't have much time to work on the rest of the function till next week (Finals Tuesday-Friday), but when I get time to, I'll finish it and post the completed function.


...

Offline

Board footer

Powered by FluxBB