Solved

question regarding SPL iterator

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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

626 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