note 82815 modified in function.array-splice by thiago
| From: | thiago@php.net | Date: | Thu, 24 Jul 2008 17:20:10 +0000 |
| Subject: | note 82815 modified in function.array-splice by thiago | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-142668@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?
<?php
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...
<?php
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...
--was--
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...
http://php.net/manual/en/function.array-splice.php