note 82815 added to function.array-splice
| From: | Francis at osu1 dot php dot net | Date: | Sun, 27 Apr 2008 07:27:35 +0000 |
| Subject: | note 82815 added to function.array-splice | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-138840@lists.php.net to get a copy of this message | ||
Do you need to sort a 2D array on just one of its variables while trying to preserve somewhat the
original order?
<?
function sort_2d_array($array, $position, $order = "ASC"){
if (!is_array($array)) return $array;
if (count($array) < 2) return $array;
$new = array($array[0]);
for ($cnt = 1; $cnt <= count($array) - 1; $cnt++){
$stop = 0;
$splice = 0;
for ($newcnt = 0; $newcnt <= count($new) - 1; $newcnt++){
if ($stop == 0){
if ($order == "ASC")
if ($array[$cnt][$position] < $new[$newcnt][$position]){
$splice = $newcnt;
$stop = 1;
} // splice position for ASC
if ($order == "DESC")
if ($array[$cnt][$position] > $new[$newcnt][$position]){
$splice = $newcnt;
$stop = 1;
} // splice position for DESC
} // stop vying for position
} // cycle through new array to find position
if ($stop == 0){
$new[] = $array[$cnt];
} else {
array_splice($new, $splice, 0, array($array[$cnt]));
} // splice into new array while keeping somewhat the original order
} // cycle through original array
return $new;
} // sort_2d_array
?>
Application Example: In-House Search Engine
Here we are trying to find the word apple in the website by sort of the most recent occurances
first, but the number of occurances first.
We've already sorted the mysql output by the date desc and have counted the no of occurances
and have placed those in an array for the final query.
I've used this function to further sort the occurances but somewhat keep the original mysql
sort order.
Key
[0] Record number
[0] Record ID
[1] Source Table
[2] No of Occurances Pinged
---------------------------
[0]
[0] 24530
[1] Blogs
[2] 1
[1]
[0] 24400
[1] Blogs
[2] 1
[2]
[0] 24240
[1] Blogs
[2] 4
[3]
[0] 243422
[1] Classifieds
[2] 1
[4]
[0] 243100
[1] Classifieds
[2] 1
After running...
sort_2d_array($array, 2, "DESC");
We have...
[0]
[0] 24240
[1] Blogs
[2] 4
[1]
[0] 24530
[1] Blogs
[2] 1
[2]
[0] 24400
[1] Blogs
[2] 1
[3]
[0] 243422
[1] Classifieds
[2] 1
[4]
[0] 243100
[1] Classifieds
[2] 1
Might be useful to someone...
----
Server IP: 208.69.120.35
Probable Submitter: 70.192.251.179
----
Manual Page -- http://www.php.net/manual/en/function.array-splice.php
Edit -- https://master.php.net/note/edit/82815
Del: integrated -- https://master.php.net/note/delete/82815/integrated
Del: useless -- https://master.php.net/note/delete/82815/useless
Del: bad code -- https://master.php.net/note/delete/82815/bad+code
Del: spam -- https://master.php.net/note/delete/82815/spam
Del: non-english -- https://master.php.net/note/delete/82815/non-english
Del: in docs -- https://master.php.net/note/delete/82815/in+docs
Del: other reasons-- https://master.php.net/note/delete/82815
Reject -- https://master.php.net/note/reject/82815
Search -- https://master.php.net/manage/user-notes.php