RecursiveArrayIterator - Recreating jQuery $.extend -- fail

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

RecursiveArrayIterator - Recreating jQuery $.extend -- fail

Post by infolock »

All,
I really don't know where to go from here. What I'm trying to do is recreate jQuery's $.extend method. Obviously, you'd have to be familiar with this, but I'm sure most will be...

Anyways, the issue I'm having is honestly just a lack of understanding with php's RecursiveArrayIterator. As you can see, I'm mainly just wanting to deal with array's.

The concept is:
  1. define default values for all required keys with the $properties array
  2. $properties can be an unlimited multi-dimensional array
  3. User defines their own array, overriding only the key/value pairs that they want to define (see line below the class definition)
  4. If a key wasn't defined or set with value, the defaults are used instead.
The following snip of code can only go 2 levels deep....and it's when a $key param. Obviously, the below $properties array won't work unless you change Prop3's subProp3 value to a string instead of an array. This is because I don't know how to keep track of the depth i'm in with this implementation...

Any help would seriously be welcomed, even if it is a url to a tutorial that I may have missed, code updates, or anything else... Thanks!

:banghead:

Code: Select all

<?php
class extend {
  private static $properties = array("prop1" => "someVal",
                                     "prop2" => array("subProp"  => "someVal",
                                                      "subProp2" => "someVal",
                                                     ),
                                     "prop3" => array("subProp"  => "someVal",
                                                      "subProp2" => "someVal",
                                                      "subProp3" => array("deepProp" => "someVal",
                                                                         ),
                                                     ),
                                    );

  /**
   * @author Jonathon Hibbard
   * Merges passed properties with the default properties.
   *
   * @param RecursiveArrayIterator $arrayObj
   * @param $key // This is used when we go to the second level...
   *
   * @todo make this an infinite recursive extend method...
   */
  public static function defaults(RecursiveArrayIterator $arrayObj, $key = NULL) {
    while($arrayObj->valid()) {
      if($arrayObj->hasChildren()) {
        self::extend($arrayObj->getChildren(), $arrayObj->key());
      } else {
        if(isset($key)) {
          self::$properties[$key][$arrayObj->key()] = $arrayObj->current();
        } else {
          self::$properties[$arrayObj->key()] = $arrayObj->current();
        }
      }
      $arrayObj->next();
    }
  }
}

$arrayObj = new RecursiveArrayIterator($myOverrideArray);
iterator_apply($arrayObj, 'extend::defaults', array($arrayObj)); // Merge our defaults with the passed properties...
?>
fyi, if anyone sees this: http://www.php.net/manual/en/function.a ... php#104739

This is not what i'm looking for.... I believe it can be done a little more elegant (not to knock that author's attempt...)
SystemAddict
Forum Newbie
Posts: 1
Joined: Fri Jul 29, 2011 5:36 am

Re: RecursiveArrayIterator - Recreating jQuery $.extend -- f

Post by SystemAddict »

You can use this code but you will need to modify it for recirsive functionality.

It does give you the jquery style extend functionality nicely though

Code: Select all



function extender($row,$json) { 
foreach($row['settings'] as $key=>$val) {
		
				if(!empty($json['settings'][$key])) { // you may want to do deeper checks on this - I didn't need to ....
					$row['settings'][$key]=$json['settings'][$key];
				}
			}
return $row;
//will return array(settings=>array('key1'=>'value1');
}
$json=array('settings'=>array('key1'=>'value1'));
$row=array('settings'=>array('key1'=>'value2'));
$a=extender($row,$json);
print_r($a);
Hope this helps ..

Alex
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Re: RecursiveArrayIterator - Recreating jQuery $.extend -- f

Post by infolock »

Thanks Alex. I guess what I'm wondering if is there a way to do with this with the RecursiveArrayIterator or the RecursiveArrayIteratorIterator. It's my way of understanding how this thing works really. Thanks for the example though!
Post Reply