Note that there are some explanatory texts on larger screens.

plurals
  1. POCakephp sort by first item in array
    text
    copied!<p>I have an array that looks like the following:</p> <pre><code>Data = array( 'Offer' =&gt; array(), 'Country' = array('Name' =&gt; Array()) ) </code></pre> <p>Now if i want to sort on the value of the first item of the <code>Name</code> array how would i go around that?</p> <p>Ive tried this so far:</p> <pre><code> $dataArray = $originalData['data']; $dataArray = Set::sort($dataArray, '{n}.Country.Name', $direction); </code></pre> <p>However this did not work</p> <p><strong>Note im using CakePHP <code>set::Sort</code> is a part of Cakephp</strong></p> <p>Okay seems i wasnt clear enough so here is a more detailed explaination of my problem:</p> <p>So ive created my own datasource in cake that collects data from an API.</p> <p>The API returns data in a way that looks like this:</p> <pre><code>data = array( 'Offer' =&gt; array('id' =&gt; 2, 'name' = 'ExampleName'), 'Stat' =&gt; array('Stat1' = 1, 'Stat2' = 2), 'Country =&gt; array('Name' =&gt; array('name1','name2') ); </code></pre> <p>Now in order to make the user able to sort these data i have to make sure that i display correctly.</p> <p>This works fine when there is only one value i.e:</p> <pre><code> $dataArray = $originalData['data']; $dataArray = Set::sort($dataArray, '{n}.Stat.Stat1', 'ASC); $originalData['data'] = $dataArray; </code></pre> <p>However country is an array so in order to "copy" the code above i need to use the first item of the Country['Name'] array.</p> <p>to do something like this:</p> <pre><code> $dataArray = $originalData['data']; $dataArray = Set::sort($dataArray, '{n}.Country.Name', $direction); $originalData['data'] = $dataArray; </code></pre> <p>However the above code is failing ...</p>
 

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