<?php Define("KEEP_TREE", 1);
Define("KEEP_KEY", 2);
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) and !is_callable($var));}
Function array_subkey¤5•3($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_object($IDkey)
and is_callable($IDkey) ) {
$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;} if ($KeepKey)
foreach ($IDkey as $newindex => $key) {
if (is_object($key)
and 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_object($key)
and 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; }
$myarray=array(
'John' => array(
'IDinfo' => array(
'name' => array('first' => "Johnathon", 'last' => "Smith"),
'address' => array(
'number' => "1234", 'street' => "10th Street", 'city' => "Anytown", 'state' => "NY", 'country' => "USA"),
'birthday' => array('month' => "January", 'day' => "1", 'year' => "2000")),
'friends' => array(
'bipeds' => array(
array('name' => 'Jenny', 'species' => "human", 'relation' => "romance", 'duration' => "1 year"),
array('name' => 'Freddy', 'species' => "human", 'relation' => "tennis partner", 'duration' => "3 months"),
array('name' => 'Tommy', 'species' => "subhuman", 'relation' => "drinking buddy", 'duration' => "7 years")),
'quadropeds' => array(
array('name' => 'Rover', 'species' => "canine", 'relation' => "best friend", 'duration' => "9 years"),
array('name' => 'Felix', 'species' => "feline", 'relation' => "sunbathing companion", 'duration' => "7 years")),
'octopeds' => array(
array('name' => 'Boris', 'species' => "common spider", 'relation' => "insect manager", 'duration' => "4 months"))),
'antagonists' => array(
'bipeds' => array(
array('name' => 'Mr. Dunbar', 'species' => "jerkass", 'relation' => "boss", 'duration' => "11 months"),
array('name' => "Mr. Pendergrass", 'species' => "prickhead", 'relation' => "dog-owning neighbor", 'duration' => "2 years")),
'quadropeds' => array(
array('name' => "Max", 'species' => "canine", 'relation' => "yard crapping neighbor", 'duration' => "2 years"))),
'job' => array('company' => "XYZ Shoes", 'title' => "stockboy", 'payrate' => "$8.95/hour")),
'Jenny' => array(
'IDinfo' => array(
'name' => array('first' => "Jennifer", 'last' => "Gudentite"),
'address' => array(
'number' => "1269", 'street' => "Main Street", 'city' => "Anytown", 'state' => "", 'country' => "Germany"),
'birthday' => array('month' => "February", 'day' => "29", 'year' => "2001")),
'friends' => array(
'bipeds' => array(
array('name' => "Holly", 'species' => "human", 'relation' => "hairdresser", 'duration' => "5 years"),
array('name' => "Maggie", 'species' => "human", 'relation' => "tennis partner", 'duration' => "2 years")),
'quadropeds' => array(
array('name' => "Fluffy", 'species' => "feline", 'relation' => "emotional management", 'duration' => "4 years"))),
'antagonists' => array(
'bipeds' => array(
array('name' => "Roxy", 'species' => "bitch", 'relation' => "workplace slut", 'duration' => "1.5 years")),
'octopeds' => array(
array('name' => "Dripping Fang", 'species' => "wolf spider", 'relation' => "bathroom dweller", 'duration' => "1 week"))),
'job' => array('company' => "Bras are us", 'title' => "salesmodel", 'payrate' => "$19.50/hour")),
'Maggie' => array(
'IDinfo' => array(
'name' => array('first' => "Magatha", 'last' => "Kristie"),
'address' => array(
'number' => "1258", 'street' => "Hightower Rd.", 'city' => "Anytown", 'state' => "", 'country' => "England"),
'birthday' => array('month' => "March", 'day' => "15", 'year' => "2002")),
'friends' => array(
'bipeds' => array(
array('name' => "Jenny", 'species' => "human", 'relation' => "tennis partner", 'duration' => "2 years"),
array('name' => "John", 'species' => "human", 'relation' => "untold satisfation", 'duration' => "10 months"),
array('name' => "Onyx", 'species' => "blackbird", 'relation' => "dead-of-night serenades", 'duration' => "3 years")),
'quadropeds' => array(
array('name' => "Tigger", 'species' => "feline", 'relation' => "tabletop decor approval", 'duration' => "3 years"))),
'antagonists' => array(
'bipeds' => array(
array('name' => "Mr. Banish", 'species' => "human", 'relation' => "bus-driving pet-policy enforceer", 'duration' => "2.5 years")),
'omnipeds' => array(
array('name' => "Zippy", 'species' => "centipede", 'relation' => "basement artroom invader", 'duration' => "2 years"))),
'job' => array('company' => "Pies in the Sky", 'title' => "Pie Baker", 'payrate' => "$7.50/hour")) );
$myfilter_01=array('filter' => array(
NULL,
function ($A, &$indx) { $indx=NULL;
$mysubfilter=array('filter' => array(
NULL,
'/peds/',
'/./', array(
function ($A) { if ($A['species']=="human") {return $A['name'];} else return NULL; },
function ($A) { if ($A['species']=="canine") {return $A['name'];} else return NULL; }
) ));
return array_subkey¤5•3($A, 4, $mysubfilter); } ));
$myfilter_02=array('filter' => array(
NULL,
function ($A, &$indx) { $indx="names of spiders"; return array_subkey¤5•3($A, 4, array('filter' => array(
NULL,
'/peds/',
NULL,
function ($A, &$indx) { if (strpos($A['species'], "spider")!==FALSE) {$indx="name"; return $A['name'];} else return NULL; }
)), KEEP_KEY); } ));
$get_spiders=function ($A, &$indx=FALSE) { if (strpos($A['species'], "spider")!==FALSE) {$indx="name"; return $A['name']." the Spider!";}
else return NULL; };
$myfilter_03=array('filter' => array(
NULL,
function ($A, &$indx) {global $get_spiders; $indx="friendly spiders";
return array_subkey¤5•3($A, 4, array('filter' => array(
"friends",
'/peds/',
NULL,
$get_spiders)), KEEP_KEY); } ));
$myfilter_04=array('filter' => array(
NULL,
function ($A, &$indx) { $indx="two-legged friends"; return array_subkey¤5•3($A, 4, array('filter' => array(
"friends",
'/ipeds/',
NULL,
NULL)), KEEP_TREE); } ));
$myfilter_05=array('filter' => array(
NULL,
function ($A) {return array_subkey¤5•3($A, 2, array('name', 'birthday'));} ));
$myfilter_06=array('filter' => array(
NULL,
function ($A) {return array_subkey¤5•3(array_subkey¤5•3($A, 2, array('name', 'birthday')), 2, NULL);} ));
$myfilter_07=array("re-index" => array('referred to as' => "name", 'biogenetic makeup' => "species"));
$myfilter_08=function ($A, &$indx) { $indx="acquaintances"; return array_subkey¤5•3($A, 3,
function ($A) { global $myfilter_07; return array_subkey¤5•3($A, 2, $myfilter_07, KEEP_KEY); }); };
$myfilter_09=function ($A, &$indx) { $indx=NULL; return array_subkey¤5•3($A, 3,
function ($A) { global $myfilter_07; return array_subkey¤5•3($A, 2, $myfilter_07, KEEP_KEY); }); };
echo "Hello AGAIN!";
echo "<h1>filter 01</h1><pre>",var_dump(array_subkey¤5•3($myarray, 2, $myfilter_01, KEEP_KEY)),"</pre>\n";
echo "<hr><h1>filter 02</h1><pre>",var_dump(array_subkey¤5•3($myarray, 2, $myfilter_02, KEEP_KEY)),"</pre>\n";
echo "<hr><h1>filter 03</h1><pre>",var_dump(array_subkey¤5•3($myarray, 2, $myfilter_03, KEEP_KEY)),"</pre>\n";
echo "<hr><h1>get spiders</h1><pre>",var_dump(array_subkey¤5•3($myarray, 5, $get_spiders)),"</pre>\n";
echo "<hr><h1>get spiders and all relations</h1><pre>",var_dump(array_subkey¤5•3($myarray, 5, array($get_spiders, "relation"))),"</pre>\n";
echo "<hr><h1>filter 04</h1><pre>",var_dump(array_subkey¤5•3($myarray, 2, $myfilter_04, KEEP_KEY)),"</pre>\n";
echo "<hr><h1>filter 05</h1><pre>",var_dump(array_subkey¤5•3($myarray, 2, $myfilter_05, KEEP_TREE)),"</pre>\n";
echo "<hr><h1>filter 06</h1><pre>",var_dump(array_subkey¤5•3($myarray, 2, $myfilter_06, KEEP_TREE)),"</pre>\n";
echo "<hr><br><pre>",var_dump(array_subkey¤5•3($myarray, 3, array('name', 'birthday'))),"</pre>\n";
echo "<hr><br><pre>",var_dump(array_subkey¤5•3($myarray, 2, "job", KEEP_KEY)),"</pre>\n";
echo "<hr><h1>level 3, name, keep key</h1><pre>",var_dump(array_subkey¤5•3($myarray, 3, "name", KEEP_KEY)),"</pre>\n";
echo "<hr><h1>level 3, name, keep tree</h1><pre>",var_dump(array_subkey¤5•3($myarray, 3, "name", KEEP_TREE)),"</pre>\n";
echo "<hr><h1>level 5, name, keep key</h1><pre>",var_dump(array_subkey¤5•3($myarray, 5, "name", KEEP_KEY)),"</pre>\n";
echo "<hr><h1>level 5, name, keep tree</h1><pre>",var_dump(array_subkey¤5•3($myarray, 5, "name", KEEP_TREE)),"</pre>\n";
echo "<hr><h1>filter 07 without keep_key</h1><pre>",var_dump(array_subkey¤5•3($myarray, 5, $myfilter_07)),"</pre>\n";
echo "<hr><h1>filter 07 with keep_key</h1><pre>",var_dump(array_subkey¤5•3($myarray, 5, $myfilter_07, KEEP_KEY)),"</pre>\n";
echo "<hr><h1>filter 08</h1><pre>",var_dump(array_subkey¤5•3($myarray, 2, $myfilter_08, KEEP_KEY)),"</pre>\n";
echo "<hr><h1>filter 09</h1><pre>",var_dump(array_subkey¤5•3($myarray, 2, $myfilter_09, KEEP_KEY)),"</pre>\n";
Define ("MARKUP_RATE", 5.7);
Define ("SALES_TAX_RATE", 1.07);
Class SimpleInventory {
public $item;
public $wholesale;
public $taxPaid;
function __construct($item, $wholesale, $taxPaid) {
$this->item=$item;
$this->wholesale=$wholesale;
$this->taxPaid=$taxPaid; } }
Class DomesticItem Extends SimpleInventory {
private $markup_rate=1.79;
public function price() {return ($this->wholesale+$this->taxPaid)*$this->markup_rate*SALES_TAX_RATE;} }
Class ImportItem Extends SimpleInventory {
private $markup_rate=5.7;
public $exchangeRate;
function __construct($item, $wholesale, $taxPaid, $exchangeRate) {
$this->item=$item;
$this->wholesale=$wholesale;
$this->taxPaid=$taxPaid;
$this->exchangeRate=$exchangeRate; }
public function price() {
return ($this->wholesale*$this->exchangeRate+$this->taxPaid)*$this->markup_rate*SALES_TAX_RATE; } }
$myInventory=array(
'domestics' => array (
'New Mexico' => array(
new DomesticItem("leather saddle", 480, 33.60)),
'Minnesoda' => array(
new DomesticItem("wool hat", 22, 0),
new DomesticItem("wool gloves", 25, 0)),
'California' => array(
new DomesticItem("guitar", 389, 35.01))),
'imports' => array (
'SouthAmerica' => array (
'Argentina' => array(
new ImportItem("leather saddle", 60, 5.79, .89),
new ImportItem("leather bag", 10, .25, .89)),
'Peru' => array(
new ImportItem("wool hat", 2, 0, .82),
new ImportItem("wool gloves", 2, 0, .82))),
'CentralAmerica' => array(
'Mexico' => array(
new ImportItem("guitar", 280, 10.79, .37)),
'Guatamala' => array(
new ImportItem("woven belt", 7, 0, .32)))));
$myfilter_20=function($A, &$indx=FALSE) { $indx=NULL;
$filter_20f=function($A, &$indx=FALSE) {$indx=NULL; return array('item' => $A->item, 'price' => $A->price());};
return (is_array($A)) ?
array_subkey¤5•3($A, 2, $filter_20f) :
$filter_20f($A); };
$myfilter_21=function($A, &$indx=FALSE) { $indx=NULL;
$filter_21f=function($A, &$indx=FALSE) {$indx=NULL; return array(array('item' => $A->item, 'price' => $A->price()));};
return (is_array($A)) ?
array_subkey¤5•3($A, 2, $filter_21f) :
$filter_21f($A); };
echo "<hr><hr><h1>Class based examples - the power grows!</h1><hr><hr>";
echo "<pre>",var_dump(array_subkey¤5•3($myInventory, 5,
function ($A, &$indx) {$indx="price"; return $A->price();}, KEEP_KEY)),"</pre>\n";
echo "<hr><br><pre>",var_dump(array_subkey¤5•3($myInventory, 4,
function ($A, &$indx) { $indx="price";
return (is_object($A) and !is_callable($A)) ? $A->price() : NULL; }, KEEP_KEY)),"</pre>\n";
echo "<hr><h1>filter 20 with keep_key</h1><pre>",var_dump(array_subkey¤5•3($myInventory, 4, $myfilter_20, KEEP_KEY)),"</pre>\n";
echo "<hr><h1>filter 20 without keep_key</h1><pre>",var_dump(array_subkey¤5•3($myInventory, 4, $myfilter_20)),"</pre>\n";
echo "<hr><h1>filter 21 with keep_key</h1><pre>",var_dump(array_subkey¤5•3($myInventory, 4, $myfilter_21, KEEP_KEY)),"</pre>\n";
echo "<hr><h1>filter 21 without keep_key</h1><pre>",var_dump(array_subkey¤5•3($myInventory, 4, $myfilter_21)),"</pre>\n";
?>