Sorting an Array based on a Key

Heya,

I am trying to sort these array based on the date…

1: I first merge them, so it’s one big array.
2: Then how do I sort them based each items date?

$sample[0]['item']['text'] = 'sample1';
$sample[0]['item']['date'] = '1299781938';

$sample[1]['item']['text'] = 'sample2';
$sample[1]['item']['date'] = '1299781932';

$sample[2]['item']['text'] = 'sample3';
$sample[2]['item']['date'] = '1199781936';

$sample[3]['item']['text'] = 'sample4';
$sample[3]['item']['date'] = '1199782936';


$other[0]['item']['text'] = 'other1';
$other[0]['item']['date'] = '1211181938';

$other[1]['item']['text'] = 'other2';
$other[1]['item']['date'] = '1204221932';

$other[2]['item']['text'] = 'other3';
$other[2]['item']['date'] = '1119781936';

$other[3]['item']['text'] = 'other4';
$other[3]['item']['date'] = '1123782936';


$new = array_merge($sample, $other);

// I know below won't work, but Im trying do imitate something like this:
// array_mutli_sort($new, $new[]['item']['date']);


$sample[0]['item']['text'] = 'sample1';
$sample[0]['item']['date'] = '1299781938';

$sample[1]['item']['text'] = 'sample2';
$sample[1]['item']['date'] = '1299781932';

$sample[2]['item']['text'] = 'sample3';
$sample[2]['item']['date'] = '1199781936';

$sample[3]['item']['text'] = 'sample4';
$sample[3]['item']['date'] = '1199782936';


$other[0]['item']['text'] = 'other1';
$other[0]['item']['date'] = '1211181938';

$other[1]['item']['text'] = 'other2';
$other[1]['item']['date'] = '1204221932';

$other[2]['item']['text'] = 'other3';
$other[2]['item']['date'] = '1119781936';

$other[3]['item']['text'] = 'other4';
$other[3]['item']['date'] = '1123782936';


$new = array_merge($sample, $other);

// var_dump($new);

function mySort($item1, $item2)
{
   $d1=$item1['item']['date'];
   $d2=$item2['item']['date'];
   if ($d1===$d) return 0;
   return $d1<$d2 ? -1 : 1;
}

usort($new, 'mySort');

var_dump($new);

:slight_smile:

damn, scallioxtx beat me

$sample[0]['item']['text'] = 'sample1';
$sample[0]['item']['date'] = '10';

$sample[1]['item']['text'] = 'sample2';
$sample[1]['item']['date'] = '4';

$sample[2]['item']['text'] = 'sample3';
$sample[2]['item']['date'] = '7';

$sample[3]['item']['text'] = 'sample4';
$sample[3]['item']['date'] = '2';


$other[0]['item']['text'] = 'other1';
$other[0]['item']['date'] = '1';

$other[1]['item']['text'] = 'other2';
$other[1]['item']['date'] = '5';

$other[2]['item']['text'] = 'other3';
$other[2]['item']['date'] = '6';

$other[3]['item']['text'] = 'other4';
$other[3]['item']['date'] = '4';


$new = array_merge($sample, $other);

foreach ($new as $key => $row) {
    $date[$key]  = $row['item']['date'];
}

array_multisort($date, SORT_DESC, $new);

Thanks guys i see how its done now!!!