Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

question regarding SPL iterator

Posted on 2009-04-03
5
Medium Priority
?
272 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 1000 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:Richard Quadling
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:Richard Quadling
Richard Quadling earned 1000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

704 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