PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
works, but i would like to sort the list by $score, from high to low. what would be the best way to accomplish this? i could push $twitter_screen_name and $score into another array and then sort that... but isnt it possible to do it without creating the extra array?
Try usort or (and I think it'll work in your case) array_multisort. The first requires that you write a function to sort two $json['users'][___] arrays and the second needs you to pass in all the various array bits (users/twitter_screen_name, users/kscore, etc).
tasairis wrote:Try usort or (and I think it'll work in your case) array_multisort. The first requires that you write a function to sort two $json['users'][___] arrays and the second needs you to pass in all the various array bits (users/twitter_screen_name, users/kscore, etc).
thank you. would you mind elaborating a bit on that?
function usercmp(array $a, array $b) {
// sort first by score
if ($a["kscore"] != $b["kscore"]) return $a["kscore"] - $b["kscore"];
// otherwise use the screen name
return strcmp($a["twitter_screen_name"], $b["twitter_screen_name"]);
}
Give $json and the name of that function to usort() and you should be good to go.
function usercmp(array $a, array $b) {
// sort first by score
if ($a["kscore"] != $b["kscore"]) return $a["kscore"] - $b["kscore"];
// otherwise use the screen name
return strcmp($a["twitter_screen_name"], $b["twitter_screen_name"]);
}
Give $json and the name of that function to usort() and you should be good to go.
i tried array_multisort. i tried usort. i cant get it to work. i get "Warning: usort() [function.usort]: Invalid comparison function in" I need more guidance than that. where do i call usort? before, in or after the loop I put in the OP?
do i need to push the dimension $json['users'] into a new array or are you telling me to apply usort to directly to $json. if yes, do i apply it to $json or $json['users']?
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
AbraCadaver wrote:You can use usort() if you don't care about the numeric keys being renumbered, or if you do use uasort(). Either way it is much simpler:
For usort() you would use it right before your original foreach() in the first post. As for multisort, your second foreach() is wrong, use the original from your first post.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
AbraCadaver wrote:For usort() you would use it right before your original foreach() in the first post. As for multisort, your second foreach() is wrong, use the original from your first post.
Because that's not the original loop from your first post.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
<?php
header('Content-Type: text/plain');
// Sample array;
// Scores are strings here, but integer scores would work too
$json['users'] = array (
array (
'twitter_screen_name' => 'abc',
'kscore' => '789'
),
array (
'twitter_screen_name' => 'ghi',
'kscore' => '123'
),
array (
'twitter_screen_name' => 'def',
'kscore' => '1234'
),
array (
'twitter_screen_name' => 'jkl',
'kscore' => '123'
),
array (
'twitter_screen_name' => 'mno',
'kscore' => '456'
),
array (
'twitter_screen_name' => 'abc',
'kscore' => '789'
)
);
// Compares user arrays $a and $b based on multi-column sorting rules;
// Returns -1 if $a should be placed before $b
// or 1 if $a should be placed after $b
function compareUsers ($a, $b) {
// A set of rules to judge how $a and $b compare;
// $rules doesn't change, so it's static (shared among function calls);
// The first part of a rule is an array key to match in $a and $b;
// The second part of a rule is 1 to sort ascending, -1 descending
static $rules = array (
array ('kscore', -1),
array ('twitter_screen_name', 1)
);
// Always starts with the first rule (resets internal array pointer)
// or skips this block if there are no rules
if (reset($rules)) {
// Tries each rule until $a and $b are found to be unequal
// or the rules are exhausted
do {
// Pulls parts of current rule into $key and $order
list ($key, $order) = current($rules);
// If $a and $b are unequal based on current rule...
if ($a[$key] != $b[$key]) {
// Breaks out of the function,
// returning the inverse of $order if $a < $b
// or $order if $a > $b
return (($a[$key] < $b[$key]) ? -$order : $order);
}
// If $a and $b are equal based on current rule, tries next rule
// or breaks out of loop if there are no more rules
} while (next($rules));
}
// Rules are exhausted;
// $a and $b must be equivalent;
// Arbitrarily returns -1
return -1;
}
// Sorts users array based on the comparison function
usort($json['users'], 'compareUsers');
// Displays the sorted array
print_r($json);
terra wrote:maybe i am just too worried about efficient code for a beginner....
Worry about writing correct code. You can refactor and optimize later.
works, but i would like to sort the list by $score, from high to low. what would be the best way to accomplish this? i could push $twitter_screen_name and $score into another array and then sort that... but isnt it possible to do it without creating the extra array?
Thanks Benjamin and McInfo. AbaCadaver's solution works.
The only remaining issue was array_unique, but I confirmed that array_unique doesn't work with multi-dimensional arrays - there is a bug in array_unique. more info at: