Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Assuming it's something of this sort:</p> <pre><code>$ary = Array( 0 =&gt; Array( 'parent_category_id' =&gt; null, 'sort_order' =&gt; 0, 'name' =&gt; 'my-category-name0' ), 1 =&gt; Array( 'parent_category_id' =&gt; 0, 'sort_order' =&gt; 1, 'name' =&gt; 'my-category-name1' ), 2 =&gt; Array( 'parent_category_id' =&gt; 0, 'sort_order' =&gt; 2, 'name' =&gt; 'my-category-name2' ), 3 =&gt; Array( 'parent_category_id' =&gt; null, 'sort_order' =&gt; 0, 'name' =&gt; 'my-category-name3' ), 4 =&gt; Array( 'parent_category_id' =&gt; 3, 'sort_order' =&gt; 0, 'name' =&gt; 'my-category-name4' ) ); </code></pre> <p>You can use a combination of a foreach and <a href="http://www.php.net/manual/en/function.usort.php" rel="nofollow">usort</a> to achieve what you're going for.</p> <pre><code>// @array: the array you're searchign through // @parent_id: the parent id you're filtering by function getFromParent($array, $parent_id){ $result = Array(); foreach ($array as $category_id =&gt; $entry){ if ($entry['parent_category_id']===$parent_id) $result[$category_id] = $entry; } usort($result,create_function('$a,$b','return ($a["sort_order"]&gt;$b["sort_order"]?1:($b["sort_order"]&lt;$a["sort_order"]?-1:0));')); return $result; } var_export(getFromParent($ary,0)); </code></pre> <p><strong>EDIT</strong> Sorry, fixed some syntax errors. Tested, and works (at least to result in what I was intending)<br> <strong>EDITv2</strong> Here's the raw output from the above:</p> <pre><code>array ( 0 =&gt; array ( 'parent_category_id' =&gt; 0, 'sort_order' =&gt; 1, 'name' =&gt; 'my-category-name1', ), 1 =&gt; array ( 'parent_category_id' =&gt; 0, 'sort_order' =&gt; 2, 'name' =&gt; 'my-category-name2', ), ) </code></pre> <p>(Used var_export just for you @FelixKling)</p> <p><strong>EDITv3</strong> I've updated my answer to go along with the OP's update. I also now make it retain the original "category_id" values in the result array.</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.
    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