Here is what I want to do:
$newArray = array();
foreach($student as $s){
$newArray[$s->id][$s->grade] = $s;
}
I want to sort the students by their grades (more of a group than a sort) but I just want the grades to be sorted not the id. I could have don't this:
$newArray[$s->id] = $s->grade
asort($newArray)
but I need the remaining data in $s. Also, there is huge chunk of data associated with each student which I want to maintain.
How can I achieve such a sorting?
Edit:
Sine you're working in a framework, best declare your sort callback as a member function (inside the same class as where you'll be needing it, of course):
private function sortCB(array $a, array $b)
{//the array type hinting in arguments is optional
$i = array_keys($a);//but highly recommended
$j = array_keys($b);
if (end($i) === end($j))
{
return 0;
}
//replace '>' with '<' if you want to sort descending
return (end($i) > end($j) ? 1 : -1);//this is ascending
}
Then, in the method where the actual sorting is needed:
uasort($theArray,array($this,'sortCB'));
For more examples, see the docs. I've added a full class example at the end of this (bulky) answer
I've tried this on writecodeonline, which isn't all too good at this kind of stuff, but this did work:
$foo = array_fill_keys(array('foo','bar','q','Bond'),array());
$i = '256';
foreach($foo as $k=>$v)
{
$foo[$k][$i] = $k;
$i = (string)((int)$i%2 === 0 ? ((int)$i/2)+1 : (int)$i*3);
}
function sortCB($a,$b)
{
$i = array_keys($a);
$j = array_keys($b);
if (end($i) === end($j))
{
return 0;
}
return (end($i) > end($j) ? 1 : -1);
}
uasort($foo,'sortCB');
var_dump($foo);
But since you're using a framework, you might do well declaring that function as a member function private function sortCB(array $a,array $b), and use it like so:
uasort($foo,array($this, 'sortCB'));
There might be some more info on how best to use this callback function in a class context here
Full example + usage (tested and working):
class test
{
public $foo = null;
public function __construct()
{
$this->foo = array_fill_keys(array('foo','bar','q','Bond'),array());
$i = '256';
foreach($this->foo as $k=>$v)
{
$this->foo[$k][$i] = $k;
$i = (string)((int)$i%2 === 0 ? ((int)$i/2)+1 : (int)$i*3);
}
}
private function sortCB($a,$b)
{
$i = array_keys($a);
$j = array_keys($b);
if (end($i) === end($j))
{
return 0;
}
return (end($i) > end($j) ? 1 : -1);
}
public function sortFoo()
{
uasort($this->foo,array($this,'sortCB'));
print_r($this->foo);
return $this->foo;
}
}
$bar = new test();
$arr = $bar->sortFoo();
You can do something like:
foreach($student as $s){
$newArray[$s->id] = $s;
}
usort($newArray, function ($a, $b) { return $a->grade - $b->grade; });
Edit
For later versions that don't support anonymous functions you can define comparison function first:
function sortByGrade($a, $b)
{
return $a->grade - $b->grade;
}
usort($newArray, 'sortByGrade');
But if you get this data from db it would be easier to order it in your sql query. If you use ORM you can use its associated method.
Related
I'm try to solve a task which uses new functions php7 uniform variable syntax nested () support foo()() (https://wiki.php.net/rfc/uniform_variable_syntax).
I need write function test for this code:
$sum = function($a, $b) { return $a + $b; };
test(6)(2)(3)($sum); // 11
test(3)(1)($sum); // 4
test(3)(3)('pow'); // 27
I don't found any explanation for this feature. Where can I find how to use it? I see that I must return function name in function test, but how to pass argument?
Thanks all for help. It's something like this:
<?php
function test($a) {
echo '<br/>';
$arr[] = $a;
return $mf = function($b) use(&$mf, &$a, &$arr) {
if(gettype($b) == 'object') {
echo(array_reduce($arr, $b));
} elseif (gettype($b) == 'string') {
if($b == 'pow') {
echo array_reduce($arr, function ($carry, $a) {
return !empty($carry) ? pow($carry, $a) : $a;
});
}
} elseif (gettype($b) == 'integer') {
$arr[] = $b;
}
return $mf;
};
}
$sum = function($a, $b) { return $a + $b; };
test(6)(2)(3)($sum); // 11
test(3)(1)($sum); // 4
test(3)(3)('pow'); // 27
This is more about nested recursive functions, or currying, than that rfc. That rfc just enabled the syntax that supported it.
This uses recursion until you pass a callable:
function test($var) {
$values = [$var];
$function = function($callback) use (&$values, &$function) {
if (is_callable($callback)) {
return array_reduce(array_slice($values, 1), $callback, $values[0]);
}
$values[] = $callback;
return $function;
};
return $function;
}
Because your functions expect two parameters but your nesting could have unlimited parameters, it's best to use an array and array reduce.
However, since multiplication functions like pow won't work with a null initial value, you can specify the initial value as the first passed parameter from the array.
What is the php implementation of underscore's _.findWhere({key:'val'})?
This is how the method is documented by Underscorejs.org:
_.findWhere(list, properties)
Looks through the list and returns the first value that matches all of the key-value pairs listed in properties.
I wrote these little functions. Might be handy.
function where($list, $props)
{
$result = array_filter(
$list,
function ($e) use ($props)
{
$count = 0;
foreach ($props as $key => $value)
{
if ($value == $e[$key])
{
$count += 1;
}
return $count == count($props);
}
}
);
return $result;
}
function findWhere($list, $props)
{
$result = where($list, $props);
return array_values($result)[0];
}
There is none. Underscore is a collection of functions written in JS. It has nothing to do with PHP.
I am using a uasort function like this:
uasort($entity_list, 'sortArray');
function sortArray($a, $b) {
if($a['fixed_column_name'] == $b['fixed_column_name']) {
return 0;
}
return ($a['fixed_column_name'] < $b['fixed_column_name']) ? -1 : 1;
}
I would like to pass a parameter to the sortArray function, like this:
uasort($entity_list, 'sortArray($arg)');
function sortArray($a, $b, $arg) {
$larg = $arg;
if($a[$larg] == $b[$larg]) {
return 0;
}
return ($a[$larg] < $b[$larg]) ? -1 : 1;
}
If you're using PHP 5.3+ (and you really should at this point in time), you can use closures:
uasort($entity_list, function ($a, $b) use ($arg) {
if ($a[$arg] == $b[$arg]) {
return 0;
}
return ($a[$arg] < $b[$arg]) ? -1 : 1;
});
Otherwise, you'll have to work around doing the same thing using global variables (oh noes!) or a class.
You could always use a class:
$sorter = new Sorter($arg);
usort($entity_list, array($sorter, "sort")); //will use $sorter->sort as callback
class Sorter {
function __construct($arg) {
$this->arg = $arg;
}
function sort($a, $b) {
// sort using $a, $b and $this->arg
}
}
Closures are nicer though ^^
Take a look at Example #4 using a closure on the usort() page of the PHP manual. The same technique can be used with all the basic sorts, including uasort()
I have a class that is basically a wrapper to an Array and implements IteratorAggregate.
When a new object of the class is created, it stores it's value in a protected variable called $value. The value can be a string, integer, etc.. or a traversable object (like an array).
This object is "recursive" since when a traversable object (like an array) is passed to the constructor, he creates another instance of the class. Here's the constructor to clarify:
class ListItem implements \IteratorAggregate
{
protected $readOnly = false;
protected $key;
protected $value;
public function __construct($key, $value, $readOnly = false)
{
if($readOnly) $this->readOnly = true;
if(is_numeric($key)) $key = 'index'.$key;
$this->key = $key;
if (is_array($value) || $value instanceof Traversable) {
$this->value = array();
foreach($value as $k => $v) {
if(is_numeric($k)) $k = 'index'.$k;
$this->value[$k] = new ListItem($k, $v, $readOnly);
}
} else {
$this->value = $value;
}
}
public function __toString()
{
if ( is_array($this->value) ) {
return 'ListItem OBJECT(' . count($this->value) . ')';
} else {
return $this->value;
}
}
Right now, I'm trying to write a simple sorting method for the class.
To sort by Key, this works like a charm:
$success = ksort($this->value, $comparison);
but to sort by value, asort does not work since the actual value I'm trying to sort is stored inside the value property.
So I tried using uasort, like this:
$success = uasort($this->value, function ($a, $b)
{
if ($a->value == $b->value) return 0;
else if($a->value < $b->value) return -1;
else return 1;
});
but for some unclear reason i get the following error:
Warning: uasort() [function.uasort]: Array was modified by the user
comparison function in /* /* /* /ListItem.php on line 129
Q. Why does this happen if I'm just accessing $value for comparison not actually changing anything?
It seems a closure (or anonymous function) is in the global scope which means uasort could not access private or protected members of the ListItem object (although other ListItem Objects can access their sibling's private/protected properties)
this solved the problem: (casting to string)
$success = uasort($this->value, function ($objA, $objB) use ($comparison)
{
$a = (string) $objA;
$b = (string) $objB;
if($comparison == ListItem::SORT_NUMERIC) {
if (is_numeric($a)) $a = (int) $a;
if (is_numeric($b)) $b = (int) $b;
}
if ($a == $b) return 0;
else if($a < $b) return -1;
else return 1;
});
How can I make my own custom class be sortable using sort() for example?
I've been scanning the web to find any method of making a class Comparable like in Java but without much luck. I tried implementing __equals() but without luck. I've also tried with __toString(). My class looks like this:
class Genre {
private $genre;
private $count;
...
}
I want to sort them by count which is an Integer, in descending order... ($genre is a string)
You can create a custom sort method and use the http://www.php.net/manual/en/function.usort.php function to call it.
Example:
$Collection = array(..); // An array of Genre objects
// Either you must make count a public variable, or create
// an accessor function to access it
function CollectionSort($a, $b)
{
if ($a->count == $b->count)
{
return 0;
}
return ($a->count < $b->count) ? -1 : 1;
}
usort($Collection, "CollectionSort");
If you'd like to make a more generic collection system you could try something like this
interface Sortable
{
public function GetSortField();
}
class Genre implements Sortable
{
private $genre;
private $count;
public function GetSortField()
{
return $count;
}
}
class Collection
{
private $Collection = array();
public function AddItem($Item)
{
$this->Collection[] = $Item;
}
public function GetItems()
{
return $this->Collection;
}
public function Sort()
{
usort($this->Collection, 'GenericCollectionSort');
}
}
function GenericCollectionSort($a, $b)
{
if ($a->GetSortField() == $b->GetSortField())
{
return 0;
}
return ($a->GetSortField() < $b->GetSortField()) ? -1 : 1;
}
$Collection = new Collection();
$Collection->AddItem(...); // Add as many Genre objects as you want
$Collection->Sort();
$SortedGenreArray = $Collection->GetItems();
maybe you can use the function "usort":
class Genre {
private $genre;
private $count;
...
public function __construct($g, $c)
{
$this->genre=g;
$this->count=c;
}
public static function compare($a, $b)
{
if ($a->count < $b->count) return -1;
else if($a->count == $b->count) return 0;
else return 1;
}
...
}
$genres= array(
new Genre (1, 5),
new Genre (2, 2),
new Genre (3, 7)
);
usort($genres, array("Genre", "compare"));
Regards Thomas
The simplest way to do this is to simply use usort on a method within the class that accepts as input an array of matching objects. This is example 3 in the documentation linked to above. However, this is somewhat clunky and ugly.
A better way is to create a new type of array class specific to the desired objects using ArrayAccess. This will enable you to use the custom array like you'd expect but then also be able to run arbitrary sorting methods on the array.
Under the hood, you'd likely still want to use usort, but you'd be hiding that fact behind a much nicer interface that might look like this:
$array = new GenreCollection();
$array[] = new Genre(1); // Genre's constructor is a factory that can load genres via an ID
$array[] = new Genre(2);
$array[] = new Genre(3);
$array[] = new Genre(4);
$array[] = new Genre(5);
// Example Sorts
$array->SortByName();
$array->SortByDateInvented();
$array->SortByID();
$array->SortBySubGenres(); // Arranges Genres into a hierarchy where 'Death Metal' comes with other metal after 'Heavy Metal' - Add in a nest-level value for making dropdowns and other nested lists.
__toString() works for me:
class Genre
{
private $genre;
private $count;
public function __construct( $genre, $count = 0 )
{
$this->genre = $genre;
$this->count = $count;
}
public function __toString()
{
return $this->count . ' ' . $this->genre;
}
}
$collection = array(
new Genre( 'alternative', 3 ),
new Genre( 'jazz', 2 ),
new Genre( 'hiphop', 1 ),
new Genre( 'heavy metal', 1 )
);
natsort( $collection );
foreach( $collection as $genre )
{
echo $genre . "\n";
}
Produces:
1 heavy metal
1 hiphop
2 jazz
3 alternative