Sort Array From Database by Specific Key php
<p>I have the following array called posts which consists of data from my database:</p>

<pre><code>Array ( [post_id] =&gt; 29 [user_id] =&gt; 2 [post_image_url] =&gt; images/w8y46ot7bsr3cpxhunq9lg2dkmf05jzvei1a.jpg ) 1
Array ( [post_id] =&gt; 41 [user_id] =&gt; 2 [post_image_url] =&gt; images/czmhng9j7qr0ite4d2w6obvl3p1k5yasxuf8.jpg ) 1
Array ( [post_id] =&gt; 42 [user_id] =&gt; 2 [post_image_url] =&gt; images/rk5e7yc9xolsd4mbnp6t2w0zaquf8i1j3ghv.jpg ) 1
Array ( [post_id] =&gt; 39 [user_id] =&gt; 2 [post_image_url] =&gt; images/wuvhtcyksmza70641n3xb5l2rqgdp8jefoi9.jpg ) 1
Array ( [post_id] =&gt; 40 [user_id] =&gt; 14 [post_image_url] =&gt; images/41io86ln7f9gcz3ep2dmvrjbya5k0wsxqthu.jpg ) 1
</code></pre>

<p>I am trying to sort it from greatest to least by "post_id." Currently I am using the following code:</p>

<pre><code>usort($posts, function($a, $b) { 
    return $a-&gt;post_id &gt; $b-&gt;post_id ? -1 : 1;
});      
foreach($posts as $i){
     echo print_r($i). "&lt;br&gt;";
}
</code></pre>

<p>However, this is not working. Any help would be appreciated.</p>