Note that there are some explanatory texts on larger screens.

plurals
  1. POMultisort a 2 deep array
    primarykey
    data
    text
    <p>Consider the following multisort method. In this case I have a array of items with a specific start date. Example array is shown:</p> <pre><code>0 -&gt; array('title' =&gt; 'hello', 'attributes' =&gt; array('id' =&gt; 4, 'startdate' =&gt; '2013-06-11')), 1 -&gt; array('title' =&gt; 'hello second entry', 'attributes' =&gt; array('id' =&gt; 6, 'startdate' =&gt; '2013-04-11')) </code></pre> <p>You can see that the 2nd entry should come before the first. Using my call currently will not work because It only checks to depth 1 of the array.</p> <pre><code>$albums = $this-&gt;multiSort($items, "SORT_ASC", 'startdate', true); </code></pre> <p>How would be the best way to modify this method to have a depth search on the items in the array. Even better would be to be able to specific the depth key. I would like to avoid having to add additional parameters to the method. </p> <p>I could call the method like so and then write a for loop to get the key data, but having nested for loops is not something I want to do.</p> <pre><code>$albums = $this-&gt;multiSort($items, "SORT_ASC", array('attributes', 'startdate') , true); </code></pre> <p>What is the best way to optimize this method for my case?</p> <pre><code>public function multiSort($data, $sortDirection, $field, $isDate) { if(empty($data) || !is_array($data) || count($data) &lt; 2) { return $data; } foreach ($data as $key =&gt; $row) { $orderByDate[$key] = ($isDate ? strtotime($row[$field]) : $row[$field]); } if($sortDirection == "SORT_DESC") { array_multisort($orderByDate, SORT_DESC, $data); } else { array_multisort($orderByDate, SORT_ASC, $data); } return $data; } </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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