Re: Sorting multi-dimensional arrays
| From: | Tobias H. Michaelsen | Date: | Fri, 03 Nov 2000 11:34:10 +0000 |
| Subject: | Re: Sorting multi-dimensional arrays | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-23554@lists.php.net to get a copy of this message | ||
Okey, a more 'real-life'-like example of what i want:
I have a text-file, containing information about some friends; id#, name,
birthdate, telephone etc.
-person.txt-
0|:|John Doe|:|19640924|:|555-1354|:|...
1|:|Jane Pho|:|19731030|:|555-5793|:|...
2|:|Mike Gnu|:|19700227|:|555-3426|:|...
-person.txt-
And I want to read the information into a multi-dimensional array (ie. an
array of arrays), and then be able to sort the information according to a
specific column -- like if the information had been stored in a database,
and retrieved with ex. "SELECT * FROM person ORDER BY birthdate".
In the php-script, it should look something like this:
<?php
$person_temp = file("person.txt");
for($i = 0; $i<length($person_temp); $i++){
$person[$i] = split($person_temp[$i], "|:|");
}
/*
Now $person is an array of arrays:
$person = array(
array(0, "John Doe", 19640924, "555-1354",...),
array(1, "Jane Pho", 19731030, "555-5793",...),
array(2, "Mike Gnu", 19700227, "555-3426",...)
);
And now I want to sort the persons by their birthday:
*/
array_columnsort($person, 2);
/*
And now $person should contain:
$person = array(
array(0, "John Doe", 19640924, "555-1354",...),
array(2, "Mike Gnu", 19700227, "555-3426",...)
array(1, "Jane Pho", 19731030, "555-5793",...),
);
*/
while(list($key,$val) = each($person)){
echo $key.' ==> array('."\n";
while(list($key2,$val2) = each($val)){
echo "\t".$key.' ==> '.$val."\n";
}
echo ")\n";
}
?>
-Tobias-