Re: SPL Iterator and Associative Array

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Jason Karns wrote:
-----Original Message-----
From: Greg Beaver [mailto:cellog@xxxxxxx] Sent: Friday, June 02, 2006 10:39 PM
To: Jason Karns
Cc: php-general@xxxxxxxxxxxxx
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

If you're going to be using 5.1.2 in production, develop on 5.1.2. PHP doesn't guarantee backwards compatibility, especially in such a big change as 5.0.x -> 5.1.x. Better to develop on 5.1.2 from the start than to develop on 5.0.5, put the code on 5.1.2, get a bunch of errors, and then have to develop again on 5.1.2 to fix the bugs. Not to mention that any testing done with 5.0.5 is invalid since you can't be sure that things will behave the same with the different production version. You may even waste time working around bugs in 5.0.5 that don't exist in 5.1.2.

Regards, Adam Zey.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux