Here is my current code to add to the array:
foreach ($query->result() as $exp) {
$activities[] = array('type' => 'level', 'exp' => $exp->exp, 'timestamp' => $badge->timestamp);
}
How would I go about sorting this array according to the value of the 'timestamp' key?
You would need to use usort which will let you sort the array using a user-defined function.
http://www.php.net/manual/en/function.usort.php
Example:
<?php
function cmp($a, $b)
{
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
$a = array(3, 2, 5, 6, 1);
usort($a, "cmp");
I would use the function array-multisort for the actual sorting, also I would store the values differently:
foreach ($query->result() as $exp) {
$activities[$badge->timestamp] = array('type' => 'level',... );
}
array_multisort($activities, SORT_ASC, $array);
http://php.net/manual/en/function.array-multisort.php
Related
I have an array like:
$a = array(array("fghfg12" => 34),
array("dfgdf23" => 97),
array("wetw13" => 65),
array("rtyr567" => 18));
I want to sort this array by its value.
That means I want the result like:
$a = array(array("rtyr567" => 18),
array("fghfg12" => 34),
array("wetw13" => 65),
array("dfgdf23" => 97));
For this I am using:
uksort($a , function($key1, $key2) use ($res) {
return (array_search($key1, $res) > array_search($key2, $res));
});
Another method:
$arr2ordered = array() ;
foreach (array_keys($a) as $key) {
$arr2ordered[$key] = $a[$key] ;
}
But I didn't get my result;
I already tried with this also:
$price = array();
foreach ($a as $key => $row)
{
$price[$key] = $row;
}
array_multisort($price, SORT_DESC, $a);
print_r($a);
But still, I didnt get my result
Since you don't know what the key of each inner array will be, you can map current() over $a to get the first value in each inner array, then use that array of values as the first argument to array_multisort() to sort $a.
array_multisort(array_map('current', $a), $a);
Check it out: https://3v4l.org/4Zu0q
try that code
$a = array(array("fghfg12" => 34),
array("dfgdf23" => 97),
array("wetw13" => 65),
array("rtyr567" => 18));
usort($a, function($a, $b){
return array_values($a) > array_values($b);
});
But only for your structure..
I want to sort a mulitdimensional array according to a field in the inner array, like this:
$result = array(
array("first" => 1, "second" => 5),
array("first" => 3, "second" => 8),
array("first" => 6, "second" => 7),
array("first" => 6, "second" => 1)
);
sort($result,"second");
/*
$result = array(
array("first" => 6, "second" => 1),
array("first" => 1, "second" => 5),
array("first" => 6, "second" => 7),
array("first" => 3, "second" => 8)
);
*/
Is there something like the intended sort function here in PHP or do I have to reimplement that?
This one is very much simple function for sorting array.
function sort_by_key($a, $subkey) {
foreach($a as $k=>$v) {
$b[$k] = strtolower($v[$subkey]);
}
asort($b);
foreach($b as $key=>$val) {
$c[] = $a[$key];
}
return $c;
}
You can call it like this in your case:
sort_by_key($result , 'second');
use usort for this
This function will sort an array by its values using a user-supplied comparison function. If the array you wish to sort needs to be sorted by some non-trivial criteria, you should use this function.
function cmp($a, $b) {
if ($a['second'] == $b['second']) {
return 0;
}
return ($a['second'] < $b['second']) ? -1 : 1;
}
usort($array, 'cmp');
You can even sort of 'second' first and 'first' second :) (sort on 'first' if 'second' is the same)
function cmp($a, $b) {
if ($a['second'] == $b['second']) {
if ($a['first'] == $b['first']) {
return 0;
}
return ($a['first'] < $b['first']) ? -1 : 1;
}
return ($a['second'] < $b['second']) ? -1 : 1;
}
usort($array, "cmp");
I have an array of the following structure:
Array ( [0] => Array ( [event] => event1 [Weight] => 2 )
And I am trying to sort by 'Weight'. I have tried this:
function cmp($a, $b) {
if ($a['Weight'] > $b['Weight'] ){
return -1;
} else {
return 1;
}
}
But it isnt sorting by weight. It seems to be how i refer to weight but I am unsure how to do this correctly.
You can sort it like this:
uasort($array, function ($a, $b) {
$c = $a['Weight'];
$d = $b['Weight'];
if ($c == $d){
return 0;
}
else if($c > $d){
return 1;
}
else{
return -1;
}
});
<?php
// Obtain a list of columns
//$data = Your Array
foreach ($data as $key => $row) {
$weight[$key] = $row['Weight'];
}
// Sort the data with volume descending, edition ascending
// Add $data as the last parameter, to sort by the common key
array_multisort($weight, SORT_ASC, $data);
?>
I think your problem must be the function you use to do the actual sorting, Here is a complete example of how to sort in ether ascending or descending order.
$array = array(
array( 'event'=> 'something', 'Weight' => 2),
array( 'event'=> 'something', 'Weight' => 1),
array( 'event'=> 'something', 'Weight' => 10),
array( 'event'=> 'something', 'Weight' => 10),
array( 'event'=> 'something', 'Weight' => 0),
array( 'event'=> 'something', 'Weight' => 1),
array( 'event'=> 'something', 'Weight' => -10),
);
function weightCmp($isAscending = true) {
return function($a, $b) use ($isAscending) {
$diff = $a['Weight'] - $b['Weight'];
return $isAscending ? $diff : $diff * -1;
};
}
usort($array, weightCmp());
var_dump($array);
usort($array, weightCmp(false));
var_dump($array);
How can I sort an associative array by one of its values?
For example:
$arr = array(
'ted' => array( 'age' => 27 ),
'bob' => array( 'age' => 18 ),
'jay' => array( 'age' => 24 )
);
$arr = ???
foreach ($arr as $person)
echo $person['age'], ', ';
So that the output is:
18, 24, 27
This is an oversimplified example just to demonstrate my question.
I still require that $arr is an associative array.
The uasort() function allows you to specify a callback function, which will be responsible of doing the comparison between two elements -- so, should do just well, if you implement the proper callback function.
Here, you'd have to implement a callback function that will receive two arrays -- and compmare the age item :
function callback($a, $b) {
if ($a['age'] > $b['age']) {
return 1;
} else if ($a['age'] < $b['age']) {
return -1;
}
return 0;
}
Using that function in the following portion of code :
$arr = array(
'ted' => array( 'age' => 27 ),
'bob' => array( 'age' => 18 ),
'jay' => array( 'age' => 24 )
);
uasort($arr, 'callback');
var_dump($arr);
You would get you this resulting array :
array
'bob' =>
array
'age' => int 18
'jay' =>
array
'age' => int 24
'ted' =>
array
'age' => int 27
This is a classical example where PHP 5.3 anonymous functions come in handy:
uasort($arr, function($a, $b) {
return $a['age'] - $b['age'];
});
The $a['age'] - $b['age'] is a small trick. It works because the callback function is expected to return a value < 0 is $a is smaller than $b and a value > 0 if $a is bigger than $b.
Since you're sorting on a value inside a sub array, there's not a built-in function that will do 100% of the work. I would do a user-defined sort with:
http://www.php.net/manual/en/function.uasort.php
Here's an example comparison function that returns its comparison based on this value in the nested array
<?php
// Comparison function
function cmp($left, $right) {
$age1 = $left['age'];
$age2 = $right['age'];
if ($age1 == $age2) {
return 0;
}
return ($age1 < $age2) ? -1 : 1;
}
uasort($array, 'cmp');
http://www.php.net/manual/en/array.sorting.php
This particular case will involve using one of the sort methods that use a callback to sort
You're not just sorting an associative array, you're sorting an associative array of associative arrays ;)
A uasort call is what you're after
uasort($array, function ($a, $b) {
if ($a['age'] === $b['age']) {
return 0;
}
return $a['age'] > $a['age'] ? 1 : -1;
});
How would I switch the sort order between ascending/descending in the following function? All it does is order a multidimensional array by a chosen field, and then by title.
$sortby = 'date';
$orderby = 'asc';
function sort($a, $b)
{
$retval = strnatcmp($a[$sortby], $b[$sortby]);
if(!$retval) return strnatcmp($a['title'], $b['title']);
return $retval;
}
uasort($jobs, 'sort');
There is no reverse option - you'd have to create a new sort function that returns the negative of your sort function.
Simple but inefficient:
function rsort($a, $b)
{
return -1 * sort($a, $b);
}
<?php
// Comparison function
function cmp($a, $b) {
if ($a == $b) {
return 0;
}
return ($a>$b) ? -1 : 1;
}
// Array to be sorted
$array = array('a' => 5.2, 'b' => 2.2, 'c' => 1.2, 'd' => 1.2,'f' => 5.5, 'g' => 1.2, 'h' => 6.2);
print_r($array);
// Sort and print the resulting array
uasort($array, 'cmp');
print_r($array);
?>