Solved

question regarding SPL iterator

Posted on 2009-04-03
5
251 Views
Last Modified: 2012-05-06
Hi
i just found out about iterators. i have made this simple example but how can i show just one element of the object?
in procedural fashion i use echo $row['fname'] for example.
but with the iterator how cat i target just one element?

i attach the iterator test i made:

best regards

ron
$iterator = new LimitIterator($result);

	foreach ($iterator as $row)

	{

		foreach ($row as $field => $value)

		{

			echo "$field: $value<br/>";

		}

		echo '<br/>';

	}

Open in new window

0
Comment
Question by:derrida
  • 2
  • 2
5 Comments
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 250 total points
ID: 24063813
I'm not sure you can.

The whole idea of an iterator is to work your way through an entire collection. There is an iterator to array function, but since you are starting with an array that just seems pointless.

http://uk2.php.net/manual/en/function.iterator-to-array.php

0
 
LVL 40

Expert Comment

by:RQuadling
ID: 24064833
Interesting.

Are you going to be "filtering" the output to only contain 1 element from the array.

Or

Are you going to be "limiting" the output to only contain 1 element from the array.

I think both could be used to achieve the results.

http://docs.php.net/manual/en/class.filteriterator.php has a good example on 1 use.

You have to remember that the iterators are base classes. For example the limit iterator does actually do the limiting. You have to extend the limititerator and then add the limiting yourself.

Due to the lack of clear documentation and explanation on the SPL, they are not clearly understood by anyone except the people who created them.

They ARE useful, but sometimes it seems to be so much easier to DIY.

0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 24067398
I was not aware of the FilterIterator class - you always find something new....

Moving back to the question if only one element is required and it is in an array to start with then I cannot think why you would iterate to get it. If it was in the database it would seem more sensible to retrieve only that value.

"They ARE useful, but sometimes it seems to be so much easier to DIY."

Agreed.
0
 
LVL 1

Author Comment

by:derrida
ID: 24070307
Hi
thanks for the answers.
so when will it is usefull to use the iterators?  it seem not very flexible, isn`t it?
ron
0
 
LVL 40

Assisted Solution

by:RQuadling
RQuadling earned 250 total points
ID: 24077501
Oh they are VERY VERY flexible. And that is a problem. The Oxygen documentation isn't too clear to a LOT of people (http://www.php.net/~helly/php/ext/spl/).

The official PHP documentation isn't yet complete and there are not enough real world examples.

I used to use the directory iterators ...

foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)) as $o_File) {
...
}

But when checking the actual file i/o for this, I really should have been using the caching variants and even then, doing it myself (recursive scandir()), then I found it much quicker.

Emulating a filter on this was easy enough also.

And you can wrap them all up in nice classes.

I think Helly's work is superb. I can see it is clever, useful, interesting and provides many standards. But I don't see enough examples of how to apply this to my own code.


0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now