Solved

question regarding SPL iterator

Posted on 2009-04-03
5
248 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to create an extensible mechanism for linked drop downs.
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…
The viewer will learn how to dynamically set the form action using jQuery.

771 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