<?php /* PHP_functions.php written by and Copyright © 2009,2010 Joe Golembieski, SoftMoon WebWare
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/> */
Function preg_key_grep($A, $preg, $KeepKey=FALSE) { $subA=array();
$keys=preg_grep($preg, array_keys($A));
if ($KeepKey)
foreach ($keys as $k) {$subA[$k]=$A[$k];}
else
foreach ($keys as $k) {$subA[]=$A[$k];}
return $subA; }
Function is_iterateable($var) {
return (is_array($var) or is_object($var) /*PHP>=5.3*/ and !is_callable($var) /**/ ); }
Define("KEEP_TREE", 1);
Define("KEEP_KEY", 2);
// ¤5•3
Function array_subkey($A, $indxLevel, $IDkey, $KeepTree=FALSE) { $KeepKey=($KeepTree & KEEP_KEY);
if ($indxLevel<2) {
if (is_array($IDkey) and is_array($IDkey['filter'])) $IDkey=array_shift($IDkey['filter']);
if ($IDkey===NULL) return $A;
// if (is_callable($IDkey)) { // non-object-oriented-PHP [DANGER! string-keyname / function-name collision]
if ( is_object($IDkey)
and /* $flag=method_exists($IDkey, 'filter') PHP < 5.3
or */ is_callable($IDkey) /* PHP >= 5.3 [PREFERRED] */ ) {
$indx=$KeepTree; $x=($flag) ? $IDkey->filter($A, $indx) : $IDkey($A, $indx);
return ($KeepKey and $x!==NULL and $indx!==NULL) ? array($indx => $x) : $x; }
else
if (is_array($IDkey)) { $subA=array();
if (is_array($IDkey['re-index'])) {$IDkey=$IDkey['re-index']; $reindex=TRUE;} //while only useful with "keep key", we process this here to allow the same filter to be used with and without the "keep key" option. Move it down one line for faster performance and strickter implementation.
if ($KeepKey)
foreach ($IDkey as $newindex => $key) {
// if (is_callable($IDkey)) { // non-object-oriented-PHP [DANGER! string-keyname / function-name collision]
if (is_object($key)
and /* PHP < 5.3 $flag=method_exists($key, 'filter') */
/*or PHP>=5.3 */ is_callable($key) ) { $indx=$KeepTree;
if (($x=($flag) ? $key->filter($A, $indx) : $key($A, $indx))!==NULL) $subA[$indx]=$x; continue; }
if (substr($key, 0, 1)=="/") {$subA=$subA+preg_key_grep($A, $key, KEEP_KEY); continue;}
if (array_key_exists($key, $A)) $subA[($reindex) ? $newindex : $key]=$A[$key]; }
else
foreach ($IDkey as $key) {
// if (is_callable($IDkey)) { // non-object-oriented-PHP [DANGER! string-keyname / function-name collision]
if (is_object($key)
and /* PHP < 5.3 $flag=method_exists($key, 'filter') */
/*or PHP>=5.3 */ is_callable($key) ) {
if (($x=($flag) ? $key->filter($A) : $key($A))!==NULL) $subA[]=$x; continue; }
if (substr($key, 0, 1)=="/") {$subA=array_merge($subA, preg_key_grep($A, $key)); continue;}
if (array_key_exists($key, $A)) $subA[]=$A[$key]; } }
else
if (substr($IDkey, 0, 1)=="/") {$subA=preg_key_grep($A, $IDkey, $KeepKey);}
else return ($KeepKey and isset($A[$IDkey])) ? array($IDkey => $A[$IDkey]) : $A[$IDkey]; }
else {
if (is_array($IDkey) and is_array($IDkey['filter']) and ($filter=array_shift($IDkey['filter']))!==NULL) {
$x=array_subkey($A, 1, $filter, $KeepKey);
if (is_array($filter) or @substr($filter, 0, 1)=="/" or $KeepKey) {
$indxLevel++; array_unshift($IDkey['filter'], NULL); }
if (is_iterateable($x) and $t=array_subkey($x, $indxLevel-1, $IDkey, $KeepTree)) $subA=$t; }
else { $subA=array();
if ($KeepTree) {
if ($KeepKey)
foreach ($A as $indx => $data) {
if (is_iterateable($data) and NULL!==($t=array_subkey($data, $indxLevel-1, $IDkey, $KeepTree))) $subA[$indx]=$t; }
else
foreach ($A as $data) {
if (is_iterateable($data) and NULL!==($t=array_subkey($data, $indxLevel-1, $IDkey, $KeepTree))) $subA[]=$t; } }
else
foreach ($A as $data) {
if (is_iterateable($data) and NULL!==($t=array_subkey($data, $indxLevel-1, $IDkey))) {
if (is_array($t)) $subA=array_merge($subA, $t); else $subA[]=$t; } } } }
return ($subA and count($subA)) ? $subA : NULL; }
?>
|