Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Advanced ksort()  (uksort?)

Posted on 2010-01-10
8
Medium Priority
?
285 Views
Last Modified: 2012-05-08
Hey people,

I have an array of objects. In each object is a counted value (tag_count). I want to sort my array's Key's based on the value of that tag_count variable. Something like MySQL can in a grouping.

See code for the array setup:
<?php
array(
    [0] => stdObject(
        [id] => 1,
        [tag_count] => 12
    ),
    [1] => stdObject(
        [id] => 2,
        [tag_count] => 2
    )
)
?>

Open in new window

0
Comment
Question by:Erwin_001
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 26277633
Something along these lines with uasort

<?php

// Comparison function
//
function cmp($a, $b) {
 
  if ($a['tag_count'] == $b['tag_count']) {
        return 0;
    }

    return ($a ['tag_count']< $b['tag_count']) ? -1 : 1;
}


// Set up your array.......
//
$array = .......

print_r($array);

// Sort and print the resulting array
uasort($array, 'cmp');
print_r($array);
?>


http://uk.php.net/uasort
0
 
LVL 2

Expert Comment

by:kostantinos1995
ID: 26277642
Give your key its tag_count value and then sort your array by key!
0
 
LVL 2

Expert Comment

by:kostantinos1995
ID: 26277651
Like this:
<?php
array(
    [0] => stdObject(
        [id] => 1,
        [tag_count] => 12
    ),
    [1] => stdObject(
        [id] => 2,
        [tag_count] => 2
    )
)

turn it into this:

array(
    [2] => stdObject(
        [id] => 1
    ),
    [12] => stdObject(
        [id] => 2
    )
)

and sort your array
you can access all the values with foreach() loop then
?>
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 750 total points
ID: 26277655
I should point out that if tag_count is a property then you will need to adjust the code above. Let us suppose

class stdObject {
     public $id;
     public $tag_count;

     ..... other stuff
}

The cmp() would be

function cmp($a, $b) {
 
  if ($a->tag_count == $b->tag_count) {
        return 0;
    }

    return ($a->tag_count < $b->tag_count) ? -1 : 1;
}

Now if the properties were private or protected rather than public, then you would need to provide a getter


class stdObject {
     private $id;
     private $tag_count;

     ..... other stuff

     function getTagCount() {
          return $this->tag_count;
     }
}

and modify the code like so


function cmp($a, $b) {
 
  if ($a->getTagCount() == $b->getTagCount() ) {
        return 0;
    }

    return (($a->getTagCount() < $b->getTagCount() ) ? -1 : 1;
}
0
 
LVL 83

Expert Comment

by:leakim971
ID: 26277747
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 26278910
Regarding this "Give your key its tag_count value and then sort your array by key!" -- any chance you will have duplicate keys?  If so, they will overwrite.
0
 
LVL 3

Author Comment

by:Erwin_001
ID: 26296168
Apologies for the late reaction. Was a bit caught up in other work too.

The solution(s) are in your posts. I will accept the solutions.

My thanks
0
 
LVL 3

Author Closing Comment

by:Erwin_001
ID: 31675172
-
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question