Advanced ksort() (uksort?)

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

LVL 3
Erwin_001Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Beverley PortlockCommented:
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
kostantinos1995Commented:
Give your key its tag_count value and then sort your array by key!
0
kostantinos1995Commented:
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
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Beverley PortlockCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
leakim971PluritechnicianCommented:
0
Ray PaseurCommented:
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
Erwin_001Author Commented:
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
Erwin_001Author Commented:
-
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

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.