note 92396 modified in function.array-search by danbrown
| From: | danbrown@php.net | Date: | Mon, 20 Jun 2011 15:15:04 +0000 |
| Subject: | note 92396 modified in function.array-search by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-180725@lists.php.net to get a copy of this message | ||
Made a function that I used to search the $_SERVER variable ( http://php.net/reserved.variables.server ) for
matches to getcwd ( http://php.net/getcwd ) excluding some
key's that I don't want.
<?php
/**
* Returns all key/values in array that match.
*
* @param $needle
* What your searching for
* @param $haystack
* Array of values
* @param $a_not
* Optional array of key names to exclude
*/
function boost_array_find($needle, $haystack, $a_not = array()) {
$out = array();
foreach($haystack as $key=>$value) {
if (strpos($value, $needle) !== FALSE) {
$good = TRUE;
foreach($a_not as $not) {
if (strpos($key, $not) !== FALSE) {
$good = FALSE;
}
}
if ($good) {
$out[$key] = $value;
}
}
}
return $out;
}
// Example Usage
$document_root = str_replace("\\", '/', getcwd());
$rejects = array('SCRIPT_FILENAME');
$out = boost_array_find($document_root, $_SERVER, $rejects);
print_r($out);
?>
Which should output this on most servers.
Array
(
[DOCUMENT_ROOT] => path/to/webroot
)
On Mosso's servers it will output this
Array
(
[PHP_DOCUMENT_ROOT] => path/to/webroot
)
--was--
Made a function that I used to search the $_SERVER variable ( http://php.net/reserved.variables.server ) for
matches to getcwd ( http://php.net/getcwd ) excluding some
key's that I don't want. Made this function in response to this http://drupal.org/node/482000 & Mosso's server
configuration. Will allow for a more generalized solution.
<?php
/**
* Returns all key/values in array that match.
*
* @param $needle
* What your searching for
* @param $haystack
* Array of values
* @param $a_not
* Optional array of key names to exclude
*/
function boost_array_find($needle, $haystack, $a_not = array()) {
$out = array();
foreach($haystack as $key=>$value) {
if (strpos($value, $needle) !== FALSE) {
$good = TRUE;
foreach($a_not as $not) {
if (strpos($key, $not) !== FALSE) {
$good = FALSE;
}
}
if ($good) {
$out[$key] = $value;
}
}
}
return $out;
}
// Example Usage
$document_root = str_replace("\\", '/', getcwd());
$rejects = array('SCRIPT_FILENAME');
$out = boost_array_find($document_root, $_SERVER, $rejects);
print_r($out);
?>
Which should output this on most servers.
Array
(
[DOCUMENT_ROOT] => path/to/webroot
)
On Mosso's servers it will output this
Array
(
[PHP_DOCUMENT_ROOT] => path/to/webroot
)
http://php.net/manual/en/function.array-search.php