-1

I know this might be duplicate question or there is an answer somewhere here in stackoverflow but i just can't make it work on my code. Here is the sample:

i have the PHP :

foreach ($load_image_array as $key) {
    foreach($key as $k) {
      echo "<pre>";
       print_r($k);
      echo "<pre>"; 
    }
  }

where when i print_r, it display like so :

Array
(
    [upload_id] => 26
    [upload_username] => user1
)
Array
(
    [upload_id] => 23
    [upload_username] => user2
)
Array
(
    [upload_id] => 25
    [upload_username] => user1
)
Array
(
    [upload_id] => 24
    [upload_username] => user2
)

I just want to sort it by [upload_id]. I tried array_multisort but i can't make it work, any pros can guide me here? Thanks!

Chivaa
  • 78
  • 1
  • 11

2 Answers2

0

You can use array_multisort

MANUAL

try this:

$upload = array();
foreach ($load_image_array as $key => $row)
{
    $upload[$key] = $row['upload_id'];
}
array_multisort($upload, SORT_DESC, $load_image_array);
Alessandro Minoccheri
  • 35,521
  • 22
  • 122
  • 171
  • i tried as you said, something like this : $upload = array(); foreach ($load_image_array as $key) { foreach($key as $k => $v) { $upload[$k] = $v['upload_id']; } } array_multisort($upload, SORT_DESC, $load_image_array); echo "
    ";
    print_r($upload);
    echo "
    "; but it just show : Array ( [0] => 25 [1] => 24 [2] => 22 ) It should be 4 element and it just return 3 of it.
    – Chivaa Nov 24 '13 at 19:01
  • try to copy my code because you have write other code, try mine code exactly it works @Chivaa – Alessandro Minoccheri Nov 24 '13 at 19:19
  • I added some codes because it's showing : Message: Undefined index: upload_id at the line $upload[$key] = $row['upload_id']; . @ Alessandro Minoccheri – Chivaa Nov 24 '13 at 19:24
0

USORT

Your array elements are array themselves. You can use a custom comparison function.

function sort_desc($item1,$item2)
{
    if ($item1['upload_id'] == $item2['upload_id']) return 0;
    return ($item1['upload_id'] < $item2['upload_id']) ? 1 : -1;
}
usort($upload,'sort_desc');
print_r($upload);

OR you can also use the below a simplified version.

<?php
   usort($upload, function($a, $b) {
       return $a->integer_property - $b->integer_property;
   });       
?>
Thalaivar
  • 23,282
  • 5
  • 60
  • 71
  • function sort_desc($item1,$item2) { if ($item1['upload_id'] == $item2['upload_id']) return 0; return ($item1['upload_id'] < $item2['upload_id']) ? 1 : -1; } foreach ($load_image_array as $key) { foreach($key as $k) { } } usort($k,'sort_desc'); echo "
    ";  
    print_r($k);  
    echo "
    ";  
      
    This doesn't work :(
    – Chivaa Nov 24 '13 at 19:16
  • WHats the issue you are facing?. – Thalaivar Nov 24 '13 at 19:24
  • The issue might be simple but i cant get it fixed, Message: Illegal string offset 'upload_id'. – Chivaa Nov 24 '13 at 19:28
  • why are doing another foreach inside a foreach... just access print_r($key). If you get this error in future " Its more like your $item1 is not an array and to debug do a var_dump($var) to know more on what data-type you are trying to access... – Thalaivar Nov 24 '13 at 20:24
  • ok, i removed another foreach and access the $key and it's kinda working but there is some problem, i have 5 items in the array and after sorting, i only have 3 of them. – Chivaa Nov 24 '13 at 23:19
  • Thanks, i do it like this and it works! function sort_desc($item1,$item2) { if ($item1['upload_id'] == $item2['upload_id']) return 0; return ($item1['upload_id'] < $item2['upload_id']) ? 1 : -1; } $array_list = array(); foreach ($load_image_array as $key) { foreach ($key as $lol){ $array_list[] = $lol; } } usort($array_list,'sort_desc'); echo "
    "; 
      print_r($array_list); 
    echo "
    ";
    – Chivaa Nov 24 '13 at 23:44