PHP Using array_multisort
The PHP array_multisort function is very powerful. With it, you sort any number of arrays so that the first array sorts and is used as a template for the sorting of the other arrays. In the example below, when the array('6','7','3') sorts so that the 3 jumps to the first element position, the array('a','b','c') array does the same, with the c jumping to the front, parallel to what the 3 did, the 30 jumping to the front in array('10','20','30'), and the z jumping to the front in array('x','y','z'). The array_multisort($myArray, $myArray1, $myArray2, $myArray3); command does the job nicely.
This can come in very handy if you have put a bunch of records into arrays, with each field getting its own array. Then the sorting on one field results in a parallel sort happening in the other fields so that each record is still correct—assuming that all the element 0s of all the arrays still represent one single record, and all the element 1s of all the arrays still represent one single record, and so on.
THE PHP CODE GENERATES THE FOLLOWING DISPLAY:
PHP Using array_multisort
$myArray=array('6','7','3');
$myArray1=array('a','b','c');
$myArray2=array('10','20','30');
$myArray3=array('x','y','z');
6 7 3
a b c
10 20 30
x y z
before multisort
3 6 7
c a b
30 10 20
z x y
after multisort: all arrays sort like 1st array
THE PHP CODE:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1252">
<TITLE>PHP Using array_multisort</TITLE>
<meta name="description" content="PHP Using array_multisort">
<meta name="keywords" content="PHP Using array_multisort,Using array_multisort,array_multisort,php,PHP,javascript, dhtml, DHTML">
</HEAD>
<body>
<h1 align=center>PHP Using array_multisort</h1>
<div style='font-size:24px;margin:0 0 0 300px'>
$myArray=array('6','7','3');<BR>
$myArray1=array('a','b','c');<BR>
$myArray2=array('10','20','30');<BR>
$myArray3=array('x','y','z');<BR><BR>
<?php
$myArray=array('6','7','3');
$myArray1=array('a','b','c');
$myArray2=array('10','20','30');
$myArray3=array('x','y','z');
echo $myArray[0]." ";
echo $myArray[1]." ";
echo $myArray[2]." ";
echo "<BR>";
echo $myArray1[0]." ";
echo $myArray1[1]." ";
echo $myArray1[2]." ";
echo "<BR>";
echo $myArray2[0]." ";
echo $myArray2[1]." ";
echo $myArray2[2]." ";
echo "<BR>";
echo $myArray3[0]." ";
echo $myArray3[1]." ";
echo $myArray3[2]." ";
echo "<BR>before multisort<BR><BR><BR>";
array_multisort($myArray, $myArray1, $myArray2, $myArray3);
echo $myArray[0]." ";
echo $myArray[1]." ";
echo $myArray[2]." ";
echo "<BR>";
echo $myArray1[0]." ";
echo $myArray1[1]." ";
echo $myArray1[2]." ";
echo "<BR>";
echo $myArray2[0]." ";
echo $myArray2[1]." ";
echo $myArray2[2]." ";
echo "<BR>";
echo $myArray3[0]." ";
echo $myArray3[1]." ";
echo $myArray3[2]." ";
echo "<BR>after multisort: all arrays sort like 1st array<BR>";
?>
</div>
</body>
</html>