note 90224 modified in function.array-slice by danbrown
| From: | danbrown@php.net | Date: | Thu, 28 Oct 2010 16:06:53 +0000 |
| Subject: | note 90224 modified in function.array-slice by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-173559@lists.php.net to get a copy of this message | ||
I was trying to find a good way to find the previous several and next several results from an array
created in a MySQL query. I found that most MySQL solutions to this problem were complex. Here is a
simple function that returns the previous and next rows from the array.
<?php
/*
** function array_surround by Jamon Holmgren of ClearSight Design
** Version 1.0 - 4/10/2009
** Please direct comments and questions to my first name at symbol clearsightdesign.com
**
** Returns an array with only the $before and $after number of results
** This is set to work best with MySQL data results
** Use this to find the rows immediately before and after a particular row, as many as you want
**
** Example usage:
** $mysql_ar is an array of results from a MySQL query and the current id is $cur_id
** We want to get the row before this one and five rows afterward
**
** $near_rows = array_surround($mysql_ar, "id", $cur_id, 1, 5)
**
** Previous row is now $near_rows[-1]
** Current row is now $near_rows[0]
** Next row is $near_rows[1] ... etc
** If there is no previous row, $near_rows[-1] will not be set...test for it with
is_array($near_rows[-1])
**
*/
function array_surround($src_array, $field, $value, $before = 1, $after = 1) {
if(is_array($src_array)) {
// reset all the keys to 0 through whatever in case they aren't sequential
$new_array = array_values($src_array);
// now loop through and find the key in array that matches the criteria in $field and $value
foreach($new_array as $k => $s) {
if($s[$field] == $value) {
// Found the one we wanted
$ck = $k; // put the key in the $ck (current key)
break;
}
}
if(isset($ck)) { // Found it!
$result_start = $ck - $before; // Set the start key
$result_length = $before + 1 + $after; // Set the number of keys to return
if($result_start < 0) { // Oops, start key is before first result
$result_length = $result_length + $result_start; // Reduce the number of keys to return
$result_start = 0; // Set the start key to the first result
}
$result_temp = array_slice($new_array, $result_start, $result_length); // Slice out the results
we want
// Now we have an array, but we want array[-$before] to array[$after] not 0 to whatever.
foreach($result_temp as $rk => $rt) { // set all the keys to -$before to +$after
$result[$result_start - $ck + $rk] = $rt;
}
return $result;
} else { // didn't find it!
return false;
}
} else { // They didn't send an array
return false;
}
}
?>
I hope you find this useful! I welcome constructive criticism or comments or of course praise ;) --
just e-mail me.
- Jamon Holmgren
--was--
EDIT: I posted this under prev() but figured it was better posted here, since it uses array_slice()
and not prev(). - JH
I was trying to find a good way to find the previous several and next several results from an array
created in a MySQL query. I found that most MySQL solutions to this problem were complex. Here is a
simple function that returns the previous and next rows from the array. Refer to http://www.clearsightdesign.com/webdesign-scripts-array_surround.php
for the latest version.
<?php
/*
** function array_surround by Jamon Holmgren of ClearSight Design
** Version 1.0 - 4/10/2009
** Please direct comments and questions to my first name at symbol clearsightdesign.com
** This function is released under the GPL - free for private or commercial use
** Please do not sell this script.
** Script homepage: http://www.clearsightdesign.com/webdesign-scripts-array_surround.php
**
** Returns an array with only the $before and $after number of results
** This is set to work best with MySQL data results
** Use this to find the rows immediately before and after a particular row, as many as you want
**
** Example usage:
** $mysql_ar is an array of results from a MySQL query and the current id is $cur_id
** We want to get the row before this one and five rows afterward
**
** $near_rows = array_surround($mysql_ar, "id", $cur_id, 1, 5)
**
** Previous row is now $near_rows[-1]
** Current row is now $near_rows[0]
** Next row is $near_rows[1] ... etc
** If there is no previous row, $near_rows[-1] will not be set...test for it with
is_array($near_rows[-1])
**
*/
function array_surround($src_array, $field, $value, $before = 1, $after = 1) {
if(is_array($src_array)) {
// reset all the keys to 0 through whatever in case they aren't sequential
$new_array = array_values($src_array);
// now loop through and find the key in array that matches the criteria in $field and $value
foreach($new_array as $k => $s) {
if($s[$field] == $value) {
// Found the one we wanted
$ck = $k; // put the key in the $ck (current key)
break;
}
}
if(isset($ck)) { // Found it!
$result_start = $ck - $before; // Set the start key
$result_length = $before + 1 + $after; // Set the number of keys to return
if($result_start < 0) { // Oops, start key is before first result
$result_length = $result_length + $result_start; // Reduce the number of keys to return
$result_start = 0; // Set the start key to the first result
}
$result_temp = array_slice($new_array, $result_start, $result_length); // Slice out the results
we want
// Now we have an array, but we want array[-$before] to array[$after] not 0 to whatever.
foreach($result_temp as $rk => $rt) { // set all the keys to -$before to +$after
$result[$result_start - $ck + $rk] = $rt;
}
return $result;
} else { // didn't find it!
return false;
}
} else { // They didn't send an array
return false;
}
}
?>
I hope you find this useful! I welcome constructive criticism or comments or of course praise ;) --
just e-mail me.
- Jamon Holmgren
http://php.net/manual/en/function.array-slice.php