0

I have the following array called posts which consists of data from my database:

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

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

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

However, this is not working. Any help would be appreciated.

4
  • How is it "not working" ? (Also you access array elements with [], not ->; Add error reporting to get useful error messages) Commented May 7, 2016 at 20:41
  • add to the query order by post_id desc Commented May 7, 2016 at 20:42
  • @Rizier123 it is "not working" because the values are not sorted in descending order by "post_id." If you look at the array, you can see the order of post_id is all over the place and not in the correct order Commented May 7, 2016 at 20:44
  • @splash58 That will not make a difference because these values are retrieved based on information in another array. Doing that makes no difference. Commented May 7, 2016 at 20:47

2 Answers 2

1

Your problem is that you are accessing the element of the array with -> as opposed to []. Change this:

usort($posts, function($a, $b) { 
    return $a->post_id > $b->post_id ? -1 : 1;
}); 

To this:

usort($posts, function($a, $b) { 
    return $a[post_id] > $b[post_id] ? -1 : 1;
});
Sign up to request clarification or add additional context in comments.

Comments

0

Ryan's "correction" regarding accessing the array elements "means well", but it has syntax errors and will spew a bunch of these:

Notice: Use of undefined constant post_id - assumed 'post_id'

Furthermore his answer is more complicated than it needs to be. Because you are sorting on the first column in your subarrays, you can just use rsort() and avoid a user-defined/custom sort method. See for yourself:

Code: (Demo)

$posts=[
    ['post_id'=>29,'user_id'=>2,'post_image_url'=>'images/w8y46ot7bsr3cpxhunq9lg2dkmf05jzvei1a.jpg'],
    ['post_id'=>41,'user_id'=>2,'post_image_url'=>'images/czmhng9j7qr0ite4d2w6obvl3p1k5yasxuf8.jpg'],
    ['post_id'=>42,'user_id'=>2,'post_image_url'=>'images/rk5e7yc9xolsd4mbnp6t2w0zaquf8i1j3ghv.jpg'],
    ['post_id'=>39,'user_id'=>2,'post_image_url'=>'images/wuvhtcyksmza70641n3xb5l2rqgdp8jefoi9.jpg'],
    ['post_id'=>40,'user_id'=>14,'post_image_url'=>'images/41io86ln7f9gcz3ep2dmvrjbya5k0wsxqthu.jpg']
];
rsort($posts);

var_export($posts);

Output:

array (
  0 => 
  array (
    'post_id' => 42,
    'user_id' => 2,
    'post_image_url' => 'images/rk5e7yc9xolsd4mbnp6t2w0zaquf8i1j3ghv.jpg',
  ),
  1 => 
  array (
    'post_id' => 41,
    'user_id' => 2,
    'post_image_url' => 'images/czmhng9j7qr0ite4d2w6obvl3p1k5yasxuf8.jpg',
  ),
  2 => 
  array (
    'post_id' => 40,
    'user_id' => 14,
    'post_image_url' => 'images/41io86ln7f9gcz3ep2dmvrjbya5k0wsxqthu.jpg',
  ),
  3 => 
  array (
    'post_id' => 39,
    'user_id' => 2,
    'post_image_url' => 'images/wuvhtcyksmza70641n3xb5l2rqgdp8jefoi9.jpg',
  ),
  4 => 
  array (
    'post_id' => 29,
    'user_id' => 2,
    'post_image_url' => 'images/w8y46ot7bsr3cpxhunq9lg2dkmf05jzvei1a.jpg',
  ),
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.