Solved

question regarding SPL iterator

Posted on 2009-04-03
5
259 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 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: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 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

751 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