Jump to content

sort 2dimension array with associative key


phprookie72

Recommended Posts

I have an array as follow

$data=array('team1'=>array('w'=>0,'l'=>1,'t'=>0,'pts'=>0', team2'=>array('w'=>1,'l'=>0,'t'=>0,'pts'=>3), 'team3'=>array('w'=>0,'l'=>0,'t'=>1,'pts'=>1) )

 

I would like to sort the data useing pts key in descending order, how would i go about doing that?

thanks.

<?php
$data=array('team1'=>array('w'=>0,'l'=>1,'t'=>0,'pts'=>0), 'team2'=>array('w'=>1,'l'=>0,'t'=>0,'pts'=>3), 'team3'=>array('w'=>0,'l'=>0,'t'=>1,'pts'=>1) );
foreach ($data as $team => $value) $tmp[] = $value['pts'];
array_multisort($data, SORT_DESC, SORT_NUMERIC, $tmp, SORT_DESC, SORT_NUMERIC);
print_r($data);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.