?
Solved

Advanced ksort()  (uksort?)

Posted on 2010-01-10
8
Medium Priority
?
281 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 82

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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

762 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