note 106764 added to arrayobject.getarraycopy
| From: | IvovonPutzer at osu1 dot php dot net | Date: | Mon, 05 Dec 2011 13:06:11 +0000 |
| Subject: | note 106764 added to arrayobject.getarraycopy | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-184900@lists.php.net to get a copy of this message | ||
If you did something like this to make your constructor multidimensional capable you will have some
trouble using getArrayCopy to get a plain array straight out of the method:
<?php
public function __construct( $array = array(), $flags = 2 )
{
// letâs give the objects the right and not the inherited name
$class = get_class($this);
foreach($array as $offset => $value)
$this->offsetSet($offset, is_array($value) ? new $class($value) : $value);
$this->setFlags($flags);
}
?>
Thatâs the way I solved it:
<?php
public function getArray($recursion = false)
{
// just in case the object might be multidimensional
if ( $this === true)
return $this->getArrayCopy();
return array_map( function($item){
return is_object($item) ? $item->getArray(true) : $item;
}, $this->getArrayCopy() );
}
?>
Hope this was useful!
----
Server IP: 69.147.83.197
Probable Submitter: 79.16.109.122
----
Manual Page -- http://www.php.net/manual/en/arrayobject.getarraycopy.php
Edit -- https://master.php.net/note/edit/106764
Del: integrated -- https://master.php.net/note/delete/106764/integrated
Del: useless -- https://master.php.net/note/delete/106764/useless
Del: bad code -- https://master.php.net/note/delete/106764/bad+code
Del: spam -- https://master.php.net/note/delete/106764/spam
Del: non-english -- https://master.php.net/note/delete/106764/non-english
Del: in docs -- https://master.php.net/note/delete/106764/in+docs
Del: other reasons-- https://master.php.net/note/delete/106764
Reject -- https://master.php.net/note/reject/106764
Search -- https://master.php.net/manage/user-notes.php