Question about PHP usort function - php

i've got a PHP script where i rearange a multidimensional array with the use of the usort()-function.
this is a sample array (print_r-output) of array $arr
Array
(
[3] => Array
(
[name] => Bjudningen
[grade] => 5
[grade_type] => calculated
[orgname] => LInvitation
[id] => 13975
)
[0] => Array
(
[name] => Coeur fidèle
[grade] => 3
[grade_type] => calculated
[orgname] => Coeur fidèle
[id] => 8075
)
[2] => Array
(
[name] => Dawsonpatrullen
[grade] => 5
[grade_type] => calculated
[orgname] => The Dawson Patrol
[id] => 13083
)
)
And this is my PHP script
function sort_movies($arr,$val){
function cmp($x, $y)
{
if ( $x[$val] == $y[$val] )
return 0;
else if ( $x[$val] < $y[$val] )
return -1;
else
return 1;
}
usort($arr, 'cmp');
return $arr;
}
$sorted = sort_movies($arr,"grade");
I want to be able to sort the array on different subkeys (i.e. name, grade,id), but it doesn't work the way i do it above. however if i change $val in the sort movies function to the value "grade" it does work, so for some reason it won't allow me to send in a vaiable as the sort parameter.
what is it i'm doing wrong?

May be try this by send index of subkey i.e. grade instead of name of subkey .

With 5.3 you can do it like this:
function create_sort($key)
{
return function($x,$y) use($key)
{
return $x[$key] - $y[$key];
};
}
$sorter = create_sort('name');
usort($arr, $sorter);

The problem is that $val is only available within the scope of the function sort_movies(), not in the scope of cmp(). You need to just declare it as global. This will pull it into scope so you can use it within the cmp() function.
function sort_movies($arr,$val){
function cmp($x, $y)
{
global $val; // <---------------------------------
if ( $x[$val] == $y[$val] )
return 0;
else if ( $x[$val] < $y[$val] )
return -1;
else
return 1;
}
usort($arr, 'cmp');
return $arr;
}
$sorted = sort_movies($arr,"grade");
http://php.net/manual/en/language.variables.scope.php

Related

Recursive ksort : not sort the array

I have a question
So I have this array :
Array
(
[2016] => Array
(
[23] => Array
(
[total_auctions] => 0
[total_price] => 0
)
[22] => Array
(
[total_auctions] => 0
[total_price] => 0
)
[21] => Array
(
[total_auctions] => 0
[total_price] => 0
)
[20] => Array
(
[total_auctions] => 0
[total_price] => 0
)
)
I want to sort recursive by key. So I create the methode :
public function sortNestedArrayAssoc($a)
{
if (!is_array($a)) {
return false;
}
ksort($a);
foreach ($a as $k=>$v) {
$this->sortNestedArrayAssoc($a[$k]);
}
return true;
}
But I get the same result, the array with the key 23 is the first and I don' really understand where is the problem. Can you help me please ? Thx in advance and sorry for my english
As John Stirling mentioned, something you could do would be to pass your arguments by reference. You can do this by using the & operator in your method argument. The syntax for that would be (with the only change being the first line):
public function sortNestedArrayAssoc(&$a)
{
if (!is_array($a)) {
return false;
}
ksort($a);
foreach ($a as $k=>$v) {
$this->sortNestedArrayAssoc($a[$k]);
}
return true;
}
This means that you are then passing the variable into your function and modifying it directly instead of what PHP does normally which is pass a copy of the variable into your function. ksort is an example of a function that uses a pass by reference in its function definition.
If you were strongly against using pass by reference, you'd have to modify your code to return your variable/array to the calling scope where you then update your array.
public function sortNestedArrayAssoc($a)
{
if (is_array($a)) {
ksort($a);
foreach ($a as $k=>$v) {
$a[$k] = $this->sortNestedArrayAssoc($v);
}
}
return $a;
}

How to arrange multiple arrays by a specific field

I have an array that has multiple arrays inside it. I am trying to arrange those arrays from the amount of greatest to least new_sales. Here is the example of the array which goes on for about 40 arrays:
Array
(
[0] => Array
(
[Tech] => Array
(
[first_name] => Anthony
[last_name] => Bisignano
)
[0] => Array
(
[new_sales] => 21
[upgrades] => 2
)
)
[1] => Array
(
[Tech] => Array
(
[first_name] => Arnold
[last_name] => Ybanez
)
[0] => Array
(
[new_sales] => 5
[upgrades] => 0
)
)
The function I am trying to use is the following:
function aasort (&$techs, $key) {
$sorter=array();
$ret=array();
reset($techs);
foreach ($techs as $ii => $va) {
$sorter[$ii]=$va[$key];
}
asort($sorter);
foreach ($sorter as $ii => $va) {
$ret[$ii]=$techs[$ii];
}
$techs=$ret;
}
aasort($test,"new_sales");
AM I using this function wrong or is there another approach I should be taking?
There are sorting functions in PHP that support user-defined comparison methods. Code like this should help you get started with that.
function cmp($a, $b)
{
if ($a[0]['new_sales'] == $b[0]['new_sales']) {
return 0;
}
return ($a[0]['new_sales'] < $b[0]['new_sales']);
}
usort($data, "cmp");
In this case you probably want to use usort() such that you can define your sorting logic.
So something like this:
usort($array, function($a, $b) {
if ($a[0]['new_sales'] > $b[0]['new_sales']) {
return -1; // note we use -1 here because we want a reverse sort
} else if ($a[0]['new_sales'] < $b[0]['new_sales']) {
return 1;
} else {
return 0;
}
});

Array sort in PHP

How can I sort a 2D array in PHP.
I want to sort on date, Array is in this format :
[result] => Array
(
[0] => Array
(
[link] => http://local/node/0
[date] => 13158505310
)
[1] => Array
(
[link] => http://local/node/1
[date] => 13158505311
)
[2] => Array
(
[link] => http://local/node/2
[date] => 13158505312
Use usort:
usort( $array, function( $a, $b ){ return $a["date"] - $b["date"]; } );
Use this
function sortByDateDesc($a, $b) {
return strcmp($a["date"], $b["date"]);
}
function sortByDateAsc($a, $b) {
if ($a['date'] == $b['date']) {
return 0;
}
return ($a['date'] > $b['date']) ? -1 : 1;
}
usort($array, 'sortByDateDesc'); //Descending order
//usort($array, 'sortByDateAsc'); //Asceding order
Use http://nl.php.net/manual/en/function.usort.php
You could also try multisort http://www.php.net/manual/en/function.array-multisort.php
may be this code helpful to you....
// Obtain a list of columns
foreach (data as key => row) {
links[key] = row['link'];
dates[key] = row['date'];
}
// Sort the data with link descending, date ascending
// Add $data as the last parameter, to sort by the common key
array_multisort(link, SORT_DESC, date, SORT_ASC, data);

Sorting array according to the values in PHP

I have the following array
[0] => Array
(
[id] => 229
[val] => 2
)
[3] => Array
(
[id] => 237
[val] => 1
)
[4] => Array
(
[id] => 238
[val] => 6
)
I need to sort this array according to the val values in the array, and do not know how to accomplish this?
function cmp($a, $b)
{
if ($a["val"] == $b["val"]) {
return 0;
}
return ($a["val"] < $b["val"]) ? -1 : 1;
}
usort($yourarray, "cmp");
Read this for more information.
array_multisort can help with this, example 3 presents a similar problem and solution.
This would help - http://www.informit.com/articles/article.aspx?p=341245&seqNum=7
You can use array_multisort()
Examples here: http://www.php.net/manual/en/function.array-multisort.php
The Example #3 Sorting database results is what you want. Might be easier if you are not familiar with callback functions and usort().
use this function to sort array accroding to your need
function sksort(&$array, $subkey="id",$sort_ascending=false)
{
if (count($array))
$temp_array[key($array)] = array_shift($array);
foreach($array as $key => $val){
$offset = 0;
$found = false;
foreach($temp_array as $tmp_key => $tmp_val)
{
if(!$found and strtolower($val[$subkey]) > strtolower($tmp_val[$subkey]))
{
$temp_array = array_merge(
(array)array_slice($temp_array,0,$offset),
array($key => $val),
array_slice($temp_array,$offset)
);
$found = true;
}
$offset++;
}
if(!$found) $temp_array = array_merge($temp_array, array($key => $val));
}
if ($sort_ascending) $array = array_reverse($temp_array);
else $array = $temp_array;
}
==========================================================================
now use this function in ur array
sksort($arrayname, "val"); /* for ascending */
sksort($arrayname, "val", true); /* for descending */

Sorting an Array of Objects in PHP In a Specific Order

I have two arrays in PHP. The first array ($author_array) is comprised of user_ids in a particular order, like so: (8, 1, 6)
The second array ($user_results) is comprised of an array of objects like so:
Array
(
[0] => stdClass Object
(
[ID] => 1
[user_login] => user1
)
[1] => stdClass Object
(
[ID] => 6
[user_login] => user6
)
[2] => stdClass Object
(
[ID] => 8
[user_login] => user8
)
)
I'd like to "sort" the second array so it's in this order, which matches the order of the values in the first array of (8, 1, 6). So it'd look like this:
Array
(
[0] => stdClass Object
(
[ID] => 8
[user_login] => user8
)
[1] => stdClass Object
(
[ID] => 1
[user_login] => user1
)
[2] => stdClass Object
(
[ID] => 6
[user_login] => user6
)
)
I'm weak on data structures. How could I do this? :-)
Thanks in advance for your help!
-Bob
Use usort and provide a custom comparison function which uses the position of the key in your "ordering" array to determine the sort order, e.g. something like:
function cmp($a, $b)
{
global $author_array;
$pos1=array_search ($a->ID, $author_array);
$pos2=array_search ($b->ID, $author_array);
if ($pos1==$pos2)
return 0;
else
return ($pos1 < $pos2 ? -1 : 1);
}
usort($user_results, "cmp");
I'm not sure whether this will be significantly slower than other examples, but it seems simpler. It may be that you could build the $user_results array as an associative one to start with, using ID as the key, then you can easily do lookups.
$hash = array();
$result = array();
foreach ($user_results as $obj) {
$hash[$obj->ID] = $obj;
}
foreach ($author_array as $id) {
$result[] = $hash[$id];
}
So I had a similar issue, but I was trying to order an object by an array (that wasn't an object). It's just not possible, so here's my workaround:
$sort_array= array(11, 4, 16, 19, 23);
$myobject = sort_categories($myobject)
function cmp($a, $b) {
if ($a->sort_key == $b->sort_key) { return 0; }
return ($a->sort_key < $b->sort_key) ? -1 : 1;
}
function sort_categories($obj) {
global $sort_array;
foreach($obj as $cat) {
$cat->sort_key = 999;
for ($i=0;$i<count($sort_array);$i++) {
if ($sort_array[$i] == $cat->term_id) $cat->sort_key = $i;
}
}
usort($obj,'cmp');
return $obj;
}
I'm looking through the object and adding a new (property?) called "sort_key" which we'll then use to sort on with usort() and cmp(). By default, the new sort_key will be 999 so it gets stuck at the end.
public static function reorganizeBykey ($objects, array $keys){
$results = array();
foreach($keys as $key){
$i=0;
foreach($objects as $object){
if($object->sourceName==$key){
$results[$i] = $object;
}
$i++;
}
}
$others = (array_diff_assoc($objects,$results));
$results = array_merge($results,$others);
return $results;
}
I hope this is helpful – I had to use this to sort an array() by some keys() and then append what doesn't match at the end.

Categories