RE: SPL Iterator and Associative Array
| From: | Jason Karns | Date: | Sat, 03 Jun 2006 03:16:28 +0000 |
| Subject: | RE: SPL Iterator and Associative Array | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-237313@lists.php.net to get a copy of this message | ||
> -----Original Message-----
> From: Greg Beaver [mailto:cellog@php.net]
> Sent: Friday, June 02, 2006 10:39 PM
> To: Jason Karns
> Cc: php-general@lists.php.net
> Subject: Re: SPL Iterator and Associative Array
>
> Jason Karns wrote:
> > I'm going to try my best to explain what I'm trying to do.
> >
> > I have my own class that has an array member. This class itself
> > implements Iterator. One of the fields in the array is itself an
> > array that I would like to iterate over. Here's some code:
> >
<snip>
>
> <snip>
> Hi Jason,
>
> The code you pasted is littered with fatal errors and bugs (I
> marked one example with "^^" above). Please paste a real
> batch of code that you've tested and reproduces the error and
> that will be much more helpful. The PHP version would be
> helpful to know as well.
>
> Greg
>
> --
> No virus found in this incoming message.
> Checked by AVG Free Edition.
> Version: 7.1.394 / Virus Database: 268.8.1/354 - Release
> Date: 6/1/2006
>
>
<?php
class Folio implements Iterator {
private $projects = array();
private $valid = FALSE;
public function __construct($file = null) {
if(!is_null($file))
$this->load($file);
}
public function load($file){
...
$keys = array();
$values = array();
foreach ($projects as $project) {
$small = array();
$big = array();
foreach
($xpath->query('showcase/images/screenshot/thumbnail',$project) as $img){
$small[] = $img->nodeValue;}
foreach
($xpath->query('showcase/images/screenshot/src',$project) as $img){
$big[] = $img->nodeValue;}
$keys[] =
$xpath->query('@id',$project)->item(0)->nodeValue;
$values[] = array(
'title'=>$xpath->query('showcase/title',$project)->item(0)->nodeValue,
'href'=>$xpath->query('livesite',$project)->item(0)->nodeValue,
'clip'=>$xpath->query('showcase/images/feature/thumbnail',$project)->item(0)
->nodeValue,
'big'=>$big,
'small'=>$small,
'text'=>$xpath->query('showcase/description',$project)->item(0)->nodeValue);
}
$this->projects = array_combine($keys,$values);
}
function &smalls($x=null){
if(is_null($x) or !key_exists($x,$this->projects)) $x =
$this->key();
return $this->projects[$x]['small'];
}
function small_src($x=null){
if(is_null($x) or !key_exists($x,$this->projects)) $x =
$this->key();
return current($this->projects[$x]['small']);
}
function small($x=null){
if(is_null($x) or !key_exists($x,$this->projects)) $x =
$this->key();
return '<a href="'.$this->small_href().'"
title="'.$this->small_title().'">'.$this->small_img($x).'</a>';
}
}
?>
<?php
reset($folio->smalls());
while($s = current($folio->smalls())){
echo $folio->small();
next($folio->smalls());
}
foreach($folio->smalls() as $s){
echo $folio->small();
}
?>
Production server will be PHP 5.1.2, developing on 5.0.5
I am also considering making my own 'project' object and having Folio have
an array of 'projects' rather than using the array of associative arrays.
Would this provide a solution?
Thanks,
Jason
Attachment: [application/x-pkcs7-signature] smime.p7s
Attachment: [application/x-pkcs7-signature] smime.p7s