php


php array / multidimensional sort


I have this array index[0] was the score and I sorted it correctly. and I'm stuck at index[3] which is some important data. coz I want it to from index[0] from high to low with the corresponding sort of index[3] from low to high. as you can see
array (0) display first but it contains the score of 20 and index[3] of 404 instead array (1) had a lower index[3]. please help thanks.
[0] => Array
(
[0] => 20
[1] => Revelyn Nazar
[2] => PASSED
[3] => 404
)
[1] => Array
(
[0] => 20
[1] => Mark Valle
[2] => PASSED
[3] => 351
)
[2] => Array
(
[0] => 20
[1] => Marita Serrano
[2] => PASSED
[3] => 372
)
[3] => Array
(
[0] => 20
[1] => Ma Lourdes Pulumbarit
[2] => PASSED
[3] => 482
)
[4] => Array
(
[0] => 20
[1] => Diana Rose Reyes
[2] => PASSED
[3] => 584
)
[5] => Array
(
[0] => 20
[1] => Andrea Reyes
[2] => PASSED
[3] => 398
)
[6] => Array
(
[0] => 19
[1] => Willie Masiclat
[2] => PASSED
[3] => 455
)
[7] => Array
(
[0] => 19
[1] => Rhaymond Emata
[2] => PASSED
[3] => 540
)
[8] => Array
(
[0] => 19
[1] => Magnolia Grace Mallari
[2] => PASSED
[3] => 516
)
[9] => Array
(
[0] => 19
[1] => Ma. Milania Castro
[2] => PASSED
[3] => 429
)
[10] => Array
(
[0] => 19
[1] => Kris Gutierrez
[2] => PASSED
[3] => 459
)
[11] => Array
(
[0] => 19
[1] => Karren Ann Cruz
[2] => PASSED
[3] => 410
)
[12] => Array
(
[0] => 19
[1] => Iwee Boy Sarita
[2] => PASSED
[3] => 451
)
[13] => Array
(
[0] => 19
[1] => Gretchen Concepcion
[2] => PASSED
[3] => 517
)
[14] => Array
(
[0] => 19
[1] => Clarissa Aguinaldo
[2] => PASSED
[3] => 439
)
[15] => Array
(
[0] => 19
[1] => Camille Jolo
[2] => PASSED
[3] => 347
)
[16] => Array
(
[0] => 19
[1] => April Buenaventura
[2] => PASSED
[3] => 600
)
[17] => Array
(
[0] => 19
[1] => Alyssa Rose Angelo
[2] => PASSED
[3] => 375
)
[18] => Array
(
[0] => 18
[1] => Joel Valencia
[2] => PASSED
[3] => 415
)
[19] => Array
(
[0] => 18
[1] => Errylyn Coronel
[2] => PASSED
[3] => 437
)
[20] => Array
(
[0] => 18
[1] => Editha Joy Paras
[2] => PASSED
[3] => 339
)
[21] => Array
(
[0] => 18
[1] => Diona Culala
[2] => PASSED
[3] => 601
)
[22] => Array
(
[0] => 18
[1] => Abbygael Aguirre
[2] => PASSED
[3] => 479
)
)
You can use uasort for sorting by more than 1 keys
uasort($your_arr, function($a,$b){
$c = $a[0] - $b[0];
$c .= $a[3] - $b[3];
return $c;
});
Result from your array
Array
(
[15] => Array
(
[0] => 19
[1] => Camille Jolo
[2] => PASSED
[3] => 347
)
[11] => Array
(
[0] => 19
[1] => Karren Ann Cruz
[2] => PASSED
[3] => 410
)
[9] => Array
(
[0] => 19
[1] => Ma. Milania Castro
[2] => PASSED
[3] => 429
)
[14] => Array
(
[0] => 19
[1] => Clarissa Aguinaldo
[2] => PASSED
[3] => 439
)
[12] => Array
(
[0] => 19
[1] => Iwee Boy Sarita
[2] => PASSED
[3] => 451
)
[6] => Array
(
[0] => 19
[1] => Willie Masiclat
[2] => PASSED
[3] => 455
)
[10] => Array
(
[0] => 19
[1] => Kris Gutierrez
[2] => PASSED
[3] => 459
Use usort:
function my_sort($a,$b)
{
if ($a[0]==$b[0]){ return $a[3] - $b[3] };
return $a[0]-$b[0];
}
$a=/*your array*/;
usort($a,"my_sort");
see here for more info:
http://www.w3schools.com/php/func_array_usort.asp

Related Links

How do i insert checkbox values from a form into the database using PDO. I've tried doing something, but that doesn't work
PHPExcel is not working in my case, the sheet is not downloading
How to remove dynamic field suffix like _i,_txt in solr while quering?
Choosing a Paypal Service
Open a file type in php
How to send the XML encoded data to the Rest API when it has & as it takes the data after “&” as new post parameter?
How to securely share a link to others - Laravel 5
custom php.ini not taking effect
can use httpClient in Android Studio?
Reverse routing in Lumen. Call Router by it's name
Uber Uploader XML ERROR: XML_ERR_NAME_REQUIRED at line 1 with hwdMediaShare
PHP Gearman Worker / Client does not work
Composer, class within my package is not being autoloaded in application using it
How to match any characters without space and don't array usefull things
How do I optimize memory usage in PHP for this function?
Using php file_get_contents() does not work when using ajax

Categories

HOME
knockout.js
verification
apple-push-notifications
angular-ui-grid
schema.org
postgresql-9.4
swi-prolog
tcsh
mongoid
bing-search
tfs2015
elk-stack
implicit-conversion
angular-meteor
bellman-ford
twitter-oauth
migrate
xbap
django-rq
closures
robolectric
unity-container
dashdb
web-push
window-managers
spreadsheetgear
splunk-query
credit-card
premake
infopath2010
fotorama
rspec-rails
mongoengine
transform
orchardcms-1.8
nanogallery
python-sounddevice
praat
best-buy-api
flipkart
coverage.py
robust
kitura
axis
adblock
qvtkwidget
photoswipe
brightscript
watir-webdriver
sesame
elastic4s
yui3
e
nsrunloop
svnserve
xcode8-beta4
adp
apache-pig-grunt
xcopy
diawi
godeps
chicagoboss
extend
search-form
bootstrap-wysiwyg
python-curses
android-audiomanager
mxe
operands
rubber
dc
iodocs
berkeley-db-xml
windows-rt
visual-c++-2012
xcode6.3.1
box2dweb
maven-central
thruway
fileshare
air-native-extension
jchartfx
sensormanager
hyphen
colon
arel
daap
nsmutablestring
server-name
hardcode
vows
startupscript
simile
hungarian-notation
cassini
eventaggregator
firephp
powergui
fxruby
database-cloning
bindable-linq

Resources

Encrypt Message