note 72790 added to ref.spl

From: Date: Thu, 01 Feb 2007 21:43:54 +0000
Subject: note 72790 added to ref.spl
Groups: php.notes 
Request: Send a blank email to php-notes+get-122489@lists.php.net to get a copy of this message
<?php /* How to store SPL Iterator results (rather than just echo-and-forget): The library of Iterators are object based, so you need to trick the little rascals into an array. Here's how (two ways) ... 1. Explicit typecasts: $a[] = (array)$Obj->objMethod(); 2. Array definition: $a[] = array( key => $Obj->objMethod() ); Examples: DirectoryIterator() */ // 1. explicity typecast object as array foreach ( new DirectoryIterator('./') as $Item ) { $fname = (array)$Item->getFilename(); $dir_listing[] = $fname[0]; } // echo "<pre>"; print_r($dir_listing); unset($dir_listing); echo"</pre><hr />"; // // or // 2. define array as key => object->method foreach ( new DirectoryIterator('./') as $Item ) { $dir_listing[] = array ( "fname" => $Item->getFilename(), "path" => $Item->getPathname(), "size" => $Item->getSize(), "mtime" => $Item->getMTime() ); } // echo "<pre>"; print_r($dir_listing); unset($dir_listing); echo"</pre>"; // ?> ---- Server IP: 216.194.113.175 Probable Submitter: 69.143.163.22 ---- Manual Page -- http://www.php.net/manual/en/ref.spl.php Edit -- https://master.php.net/note/edit/72790 Del: integrated -- https://master.php.net/note/delete/72790/integrated Del: useless -- https://master.php.net/note/delete/72790/useless Del: bad code -- https://master.php.net/note/delete/72790/bad+code Del: spam -- https://master.php.net/note/delete/72790/spam Del: non-english -- https://master.php.net/note/delete/72790/non-english Del: in docs -- https://master.php.net/note/delete/72790/in+docs Del: other reasons-- https://master.php.net/note/delete/72790 Reject -- https://master.php.net/note/reject/72790 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#122489) next »