|
Posted by "Alan Lord" on 10/13/05 23:13
Blimey...
That's going to take some de-ciphering... It looks fascinating :-)
Thanks!
Alan
> -----Original Message-----
> From: Jochem Maas [mailto:jochem@iamjochem.com]
> Sent: 13 October 2005 20:53
> To: Alan Lord
> Cc: php-general@lists.php.net
> Subject: Re: [PHP] RE: Removing Items from an Array
>
> Alan Lord wrote:
> > Hi TG and others,
> >
> > I think I must be missing something here.
> >
> > Your example doesn't seem to traverse "down" into a multidim array.
> >
> > Also, it appears as though your script assumes that the
> structure of
> > the array is known. It isn't and it is retrieved from "far
> away" and I
> > have no control over it's structure/depth/size etc... I can take a
> > good guess at the keys I want to keep and keep those in an
> array in my config.inc.
> > But I would ideally like to be able to have a function which is as
> > "array agnostic" as possible.
>
> shouldn't be 'array paragnostic' or maybe 'array gnostic' -
> psychic hypertext processor. does what you mean.
>
> >
> > I tried to do an "unset($arr[key])" in one of my other attempts at
> > solving this problem. But from what I read in the manual (if I
> > understood correctly :-)), it seems as though you can't
> really do this
> > from within a function/routine which is walking through the
> array at
> > that time as the array is not re-ordered...
>
> right so you have to write your own function. and/or maybe
> combine it with
> array_map() and/or array_filter() ... here is a routine that
> drills into an array given an array of values that act as the 'path'
> into the array your 'drilling' (can you handle php5?), may be
> that inspires you a bit:
>
> /*
> * eg
>
> $yourDataSet = array();
> $yourDataSet['A'] = array();
> $yourDataSet['B'] = array();
> $yourDataSet['A']['A'] = array();
> $yourDataSet['B']['B'] = array();
> $yourDataSet['B']['B']['coolstuff'] = array(
> 'foo' => 'bar',
> 'bar' => 'qux',
> );
>
> ArrayDriller::setSource( $yourDataSet ); $somedata =
> ArrayDriller::get( array('B','B','coolstuff') )
> */
>
> class ArrayDriller
> {
> static private $source;
>
> static public function setSource($var)
> {
> // do a check to make sure the keys are associative?
> if (is_array($var) && count($var)) self::$source = $var;
> }
>
> static public function set($varName, $value = null)
> {
> if (is_array(self::$source) && $varName &&
> !is_numeric($varName)) {
> if (is_array( $varName )) {
> $tmpArr =& self::$source;
> while ( 1 ) {
> self::chkVarName($k = array_shift( $varName ));
> if ( !count( $varName )) {
> return ($tmpArr[ $k ] = $value);
> break;
> } else if (! isset($tmpArr[ $k ]) || !
> is_array($tmpArr[ $k ])) {
> $tmpArr[ $k ] = array();
> }
>
> $tmpArr =& $tmpArr[ $k ];
> }
> } else {
> self::chkVarName($varName);
> return (self::$source[ $varName ] = $value);
> }
> }
> }
>
> static public function get($varName)
> {
> if (is_array(self::$source) && $varName) {
> if (is_array( $varName )) {
> $tmpArr =& self::$source;
> while ( 1 ) {
> self::chkVarName($k = array_shift( $varName ));
>
> /* endpoint */
> if ( !count( $varName )) {
> if (@is_array($tmpArr) &&
> array_key_exists($k, $tmpArr)) {
> return $tmpArr[ $k ];
> }
> break;
> }
> else if (!array_key_exists($k, $tmpArr) ||
> !is_array($tmpArr[ $k ]))
> {
> // we can go no deeper
> break;
> }
>
> $tmpArr =& $tmpArr[ $k ];
> }
> } else {
> self::chkVarName( $varName );
> if (array_key_exists($varName, self::$source)) {
> return self::$source[ $varName ];
> }
> }
> }
>
> return null;
> }
>
> static private function chkVarName($varName)
> {
> if (strval( $varName )) {
> return;
> } else {
> throw new Exception('backup buster, call that an
> assoc key?');
> // trigger_error(); // er? php4 anyone?
> }
> }
> }
>
>
> >
> > Thanks and no offence intended, I am just trying to
> understand... :-)
> >
> > Alan
> > --
> > Tg wrote:
> >
> > That works just as well, if you have only two levels of
> depth. Mostly
> > I was trying to illustrate how to use $key => $value in a
> foreach and
> > what to do if you need to go multiple levels down. Once you get to
> > the bottom level, then you can use isset() certainly.
> >
> > Just wanted to make sure that it was clear what to do with $key and
> > $subkey relating to the main array ($alldataarr in this example)
> > ---------snip-----------------
> >
>
>
Navigation:
[Reply to this message]
|