Created
August 3, 2010 10:40
-
-
Save pr1001/506171 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @author Peter Robinett <[email protected]> | |
* @since 2010-08-03 | |
*/ | |
function array2NewArray (array $arr) { | |
return new NewArray($arr); | |
} | |
function NewArray2array(NewArray $newArray) { | |
return $newArray->toArray(); | |
} | |
class NewArray extends ArrayIterator { | |
public function toArray() { | |
return $this->getArrayCopy(); | |
} | |
public function merge($arr) { | |
return new NewArray(array_merge($this->toArray(), $this->input2array($arr))); | |
} | |
public function filter($callback = NULL) { | |
if (is_null($callback)) { | |
return new NewArray(array_filter($this->toArray())); | |
} else { | |
return new NewArray(array_filter($this->toArray(), $callback)); | |
} | |
} | |
private function input2array($arr) { | |
if (!($arr instanceof NewArray) && !(is_array($arr))) { | |
throw new Exception('An array-like object must be given.'); | |
} | |
return ($arr instanceof NewArray) ? $arr->toArray() : $arr; | |
} | |
} | |
$arr = array2NewArray(array(1, 2, 3)); | |
print_r($arr->merge(array(4, 5, 6))); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment