note 87549 modified in function.iterator-to-array by danbrown
| From: | danbrown@php.net | Date: | Fri, 12 Dec 2008 20:42:53 +0000 |
| Subject: | note 87549 modified in function.iterator-to-array by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-148101@lists.php.net to get a copy of this message | ||
Using the boolean param :
<?php
$first = new ArrayIterator( array('k1' => 'a' , 'k2' =>
'b', 'k3' => 'c', 'k4' => 'd') );
$second = new ArrayIterator( array( 'k1' => 'X', 'k2' =>
'Y', 'Z' ) );
$combinedIterator= new AppendIterator();
$combinedIterator->append( $first );
$combinedIterator->append( $second );
var_dump( iterator_to_array($combinedIterator, false) );
?>
will output :
array(7) (
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[2]=>
string(1) "c"
[3]=>
string(1) "d"
[4]=>
string(1) "X"
[5]=>
string(1) "Y"
[6]=>
string(1) "Z"
)
<?php
var_dump( iterator_to_array($combinedIterator, true) );
?>
will output (since keys would merge) :
array(5) (
["k1"]=>
string(1) "X"
["k2"]=>
string(1) "Y"
["k3"]=>
string(1) "c"
["k4"]=>
string(1) "d"
[0]=>
string(1) "Z"
)
--was--
Using the boolean param :
<?php
$first = new ArrayIterator( array('k1' => 'a' , 'k2' =>
'b', 'k3' => 'c', 'k4' => 'd') );
$second = new ArrayIterator( array( 'k1' => 'X', 'k2' =>
'Y', 'Z' ) );
$combinedIterator= new AppendIterator();
$combinedIterator->append( $first );
$combinedIterator->append( $second );
var_dump( iterator_to_array($combinedIterator, false) );
?>
will output :
array(7) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[2]=>
string(1) "c"
[3]=>
string(1) "d"
[4]=>
string(1) "X"
[5]=>
string(1) "Y"
[6]=>
string(1) "Z"
}
<?php
var_dump( iterator_to_array($combinedIterator, true) );
?>
will output (since keys would merge) :
array(4) {
[0]=>
string(1) "X"
[1]=>
string(1) "Y"
[2]=>
string(1) "Z"
[3]=>
string(1) "d"
}
http://php.net/manual/en/function.iterator-to-array.php