0

I’m trying to sort my array by the timestamp which is in 'sort' => $date this line. I’ve tried doing it with array_multisort, but can't make it work. At the moment it just prints the $data array in the same order as normal. What am I doing wrong in array_multisort?

My timesmap looks like this:

1397755920

Here is my code:

$data = array();
for ($x=0; $x<=count($obj)-1; $x++) {
$date = strtotime(str_replace('/', '-', $obj[$x]['time']));
    $post_data = array(
    'title' => $obj[$x]['title'],
    'link' => $obj[$x]['link'],
    'image' => $obj[$x]['image'],
    'content' => $obj[$x]['content'],
    'time' => $obj[$x]['time'],
    'sort' => $date,
    'name' => $obj[$x]['name']);
     $data[] = $post_data;
}

array_multisort($data['sort'], SORT_DESC);
print_r($data);

USORT Example:

function my_sort($a,$b)
{
    if ($a==$b) return 0;
        return ($a<$b)?-1:1;
    }

for ($x=0; $x<=count($data)-1; $x++) {

    usort($data[$x]['sort'],"my_sort");

}
print_r($data);
  • 3
    Don’t use array_multisort, use `usort` with a little custom comparison function instead. – CBroe May 16 '14 at 22:23
  • But do i then first need to make a new array with only the timestamps or how i do this. i've searched and found the comparison function – user3646311 May 16 '14 at 22:34
  • No, you need no extra array – you simply let your comparison function pick the specific value out of the two array elements that it is passed as parameters to compare them. – CBroe May 16 '14 at 22:36
  • i've edited question and added the usort function i've tried at the button, but still it does not seem to work. the $data array seem unchanged – user3646311 May 16 '14 at 22:51
  • [This](http://stackoverflow.com/questions/17364127/reference-all-basic-ways-to-sort-arrays-and-data-in-php) might help you... – HamZa May 16 '14 at 22:53
  • Of course it doesn’t work this way … `usort` gets two of the elements of the array you want to sort passed as parameters – and these are themselves _arrays_ in your case, so your attempt at comparing them makes little sense – you want to access a _specific item_ in them (the timestamp value), and compare them by only that value. – CBroe May 16 '14 at 23:04

1 Answers1

0

Alternatively, yes you could use array_multisort() in this one. In your previous example, you forgot to feed another array which contains the timestamps. Consider this example:

// dummy data
$sample_timestamp = 1397755920;
$obj = array(
    array(
        'title' => 'title1',
        'link' => 'link1',
        'image' => 'image1',
        'content' => 'content1',
        'sort' => $sample_timestamp,
    ),
    array(
        'title' => 'title2',
        'link' => 'link2',
        'image' => 'image2',
        'content' => 'content2',
        'sort' => 1000,
    ),
    array(
        'title' => 'title3',
        'link' => 'link3',
        'image' => 'image3',
        'content' => 'content3',
        'sort' => $sample_timestamp+100,
    ),
);

$data = $timestamps = $post_data = array();
for($x = 0; $x < count($obj); $x++) {
    $timestamps[] = $obj[$x]['sort']; // you forgot to add another array
    $post_data = array(
        'title' => $obj[$x]['title'],
        'link' => $obj[$x]['link'],
        'image' => $obj[$x]['image'],
        'content' => $obj[$x]['content'],
        'sort' => $obj[$x]['sort']);
    $data[] = $post_data;
}

array_multisort($timestamps, SORT_DESC, $data);


print_r($data);

Sample Output:

Array
(
    [0] => Array
    (
        [title] => title3
        [link] => link3
        [image] => image3
        [content] => content3
        [sort] => 1397756020
    )

    [1] => Array
    (
        [title] => title1
        [link] => link1
        [image] => image1
        [content] => content1
        [sort] => 1397755920
    )

    [2] => Array
    (
        [title] => title2
        [link] => link2
        [image] => image2
        [content] => content2
        [sort] => 1000
    )

)    
user1978142
  • 7,946
  • 3
  • 17
  • 20