Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use not only an anonymous function but a closure, like e.g.</p> <pre><code>function ($a,$b) use ($sort) { ... } </code></pre> <p>and $sort will be available in the function body. Self-contained example:</p> <pre><code>&lt;?php function getFn($sort) { return function($a, $b) use($sort) { if($a-&gt;$sort &gt; $b-&gt;$sort) return 1; if($a-&gt;$sort &lt; $b-&gt;$sort) return -1; return 0; }; } $abuseCases = array( (object)array('ID'=&gt;1, 'Sender'=&gt;'Sender A'), (object)array('ID'=&gt;3, 'Sender'=&gt;'Sender D'), (object)array('ID'=&gt;2, 'Sender'=&gt;'Sender C'), (object)array('ID'=&gt;4, 'Sender'=&gt;'Sender B') ); echo "\n----- Sort By Sender ----\n"; usort($abuseCases, getFn('Sender')); foo($abuseCases); echo "\n----- Sort By ID ----\n"; usort($abuseCases, getFn('ID')); foo($abuseCases); function foo($a) { foreach($a as $o) { echo $o-&gt;ID, ' ', $o-&gt;Sender, "\n"; } } </code></pre> <p>prints</p> <pre><code>----- Sort By Sender ---- 1 Sender A 4 Sender B 2 Sender C 3 Sender D ----- Sort By ID ---- 1 Sender A 2 Sender C 3 Sender D 4 Sender B </code></pre> <hr> <p><strong>Update</strong>: With php&lt;5.3 you can use an object instead of an anonymous function. usort() expects the second parameter to be a <code>callable</code>. That can be an anoymous function as of php 5.3, but it can also be the name of a function ....or an object and a method name passed as an array like <code>array($obj, 'methodName')</code>.</p> <pre><code>$abuseCases = getData(); echo "\n----- Sort By Sender ----\n"; usort($abuseCases, array(new Foo('Sender'), 'compare')); foo($abuseCases); echo "\n----- Sort By ID ----\n"; usort($abuseCases, array(new Foo('ID'), 'compare')); foo($abuseCases); class Foo { public $propName; // protected? public function __construct($propertyName) { $this-&gt;propName = $propertyName; } public function compare($a, $b) { $prop = $this-&gt;propName; if($a-&gt;$prop &gt; $b-&gt;$prop) return 1; if($a-&gt;$prop &lt; $b-&gt;$prop) return -1; return 0; } } function foo($a) { foreach($a as $o) { echo $o-&gt;ID, ' ', $o-&gt;Sender, "\n"; } } function getData() { return array( (object)array('ID'=&gt;1, 'Sender'=&gt;'Sender A'), (object)array('ID'=&gt;3, 'Sender'=&gt;'Sender D'), (object)array('ID'=&gt;2, 'Sender'=&gt;'Sender C'), (object)array('ID'=&gt;4, 'Sender'=&gt;'Sender B') ); } </code></pre> <p>(If you make heavy use of this - or don't want to write excuses like this one -_- - you might want to define an interface like <code>interface Comparator { ... }</code>, let Foo implement that interface and have some function/class that uses Comparator, i.e. a wrapper for objects around usort().)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload