note 112432 added to class.iteratoraggregate

From: Date: Sun, 16 Jun 2013 11:49:02 +0000
Subject: note 112432 added to class.iteratoraggregate
Groups: php.notes 
Request: Send a blank email to php-notes+get-195239@lists.php.net to get a copy of this message
<?php // IteratorAggregate // Create indexed and associative arrays. class myData implements IteratorAggregate { private $array = []; const TYPE_INDEXED = 1; const TYPE_ASSOCIATIVE = 2; public function __construct( array $data, $type = self::TYPE_INDEXED ) { reset($data); while( list($k, $v) = each($data) ) { $type == self::TYPE_INDEXED ? $this->array[] = $v : $this->array[$k] = $v; } } public function getIterator() { return new ArrayIterator($this->array); } } $obj = new myData(['one'=>'php','javascript','three'=>'c#','java',], /*TYPE 1 or 2*/ ); foreach($obj as $key => $value) { var_dump($key, $value); echo PHP_EOL; } // if TYPE == 1 #int(0) #string(3) "php" #int(1) #string(10) "javascript" #int(2) #string(2) "c#" #int(3) #string(4) "java" // if TYPE == 2 #string(3) "one" #string(3) "php" #int(0) #string(10) "javascript" #string(5) "three" #string(2) "c#" #int(1) #string(4) "java" ?> Good luck! ---- Server IP: 69.147.83.199 Probable Submitter: 109.205.248.99 ---- Manual Page -- http://php.net/manual/en/class.iteratoraggregate.php Edit -- https://master.php.net/note/edit/112432 Del: integrated -- https://master.php.net/note/delete/112432/integrated Del: useless -- https://master.php.net/note/delete/112432/useless Del: bad code -- https://master.php.net/note/delete/112432/bad+code Del: spam -- https://master.php.net/note/delete/112432/spam Del: non-english -- https://master.php.net/note/delete/112432/non-english Del: in docs -- https://master.php.net/note/delete/112432/in+docs Del: other reasons-- https://master.php.net/note/delete/112432 Reject -- https://master.php.net/note/reject/112432 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#195239) next »