|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Hiya!
I would like to take a partial slice of a fairly complex array. The source looks something like array ( [Full Name] ==> array ( [p_id] ==> NN [field1] ==> blah [fieldN] ==> blah [numbers] ==> array { [800-555-1212] ==> "cell" [123-456-7890] ==> "home" ] [emails] ==> array ( [email@add.re.ss] ==> [e.mail@addr.ess] ==> ) ) ) for many fields 1-N across many people. I want to end up with an array like array ( [Full Name] ==> array ( [display] ==> "Full Name" [link] ==> "http://example.com/p_id=NN" ) ) combining some pieces from the source array and adding other pieces either constructed (eg link) or static (maybe). Is there an easy way to do this across my entire source array without just looping over it myself? Why would I do this, you ask? Well, I have to print a list a bunch of times and I am tired of rewriting the code :-) I'll need to be able to specify display value, link target, and/or form field name for starters; a separate array will control how to present the data so that I can end up with anything from a space-separated list to a row in a table to a bunch of checkboxes or radio buttons and so on. I'm certainly open to simpler ways to accomplish the same thing, but a function that takes data and control arrays and handles the presentation certainly seems nice at the moment :-) TIA & HAND P.S. -- This is my first post-by-google, so I'm interested in seeing how the formatting comes out. Wish me luck :-) :-D -- David T-G see http://justpickone.org/email/ |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
On 18.09.2007 06:29 David TG wrote:
> Hiya! > > I would like to take a partial slice of a fairly complex array. The > source looks something like > > array > ( > [Full Name] ==> array > ( > [p_id] ==> NN > [field1] ==> blah > [fieldN] ==> blah > [numbers] ==> array > { > [800-555-1212] ==> "cell" > [123-456-7890] ==> "home" > ] > [emails] ==> array > ( > [email@add.re.ss] ==> > [e.mail@addr.ess] ==> > ) > ) > ) > > for many fields 1-N across many people. I want to end up with an > array like > > array > ( > [Full Name] ==> array > ( > [display] ==> "Full Name" > [link] ==> "http://example.com/p_id=NN" > ) > ) > > combining some pieces from the source array and adding other pieces > either constructed (eg link) or static (maybe). > > Is there an easy way to do this across my entire source array without > just looping over it myself? There's apparently no built-in function that can handle this. If, for some reason, you're unhappy with "foreach", you can try array_map() or array_walk(). The modern OO approach would be to use an iterator, perhaps even different ones for different purposes. class DisplayIterator implements Iterator { function __construct($ary) { $this->ary = $ary; .... function next() { // get next element and format it how you want } .... foreach(new DisplayIterator($yourArray) as $element) // do whatever with $element > > Why would I do this, you ask? Well, I have to print a list a bunch of > times and I am tired of rewriting the code :-) I'll need to be able > to specify display value, link target, and/or form field name for > starters; a separate array will control how to present the data so > that I can end up with anything from a space-separated list to a row > in a table to a bunch of checkboxes or radio buttons and so on. I'm > certainly open to simpler ways to accomplish the same thing, but a > function that takes data and control arrays and handles the > presentation certainly seems nice at the moment :-) > > > TIA & HAND > > P.S. -- This is my first post-by-google, so I'm interested in seeing > how the formatting comes out. Wish me luck :-) > > :-D > -- > David T-G > see http://justpickone.org/email/ > The formatting is ok, but you should add a space after "--". -- gosha bine makrell ~ http://www.tagarga.com/blok/makrell php done right http://code.google.com/p/pihipi |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
gosha, et al --
gosha bine wrote: > On 18.09.2007 06:29 David TG wrote: > > > > I would like to take a partial slice of a fairly complex array. The > > source looks something like .... > > Is there an easy way to do this across my entire source array without > > just looping over it myself? > > There's apparently no built-in function that can handle this. If, for > some reason, you're unhappy with "foreach", you can try array_map() or Ahhhh... Those both sound familiar; I'll check 'em out. > array_walk(). The modern OO approach would be to use an iterator, > perhaps even different ones for different purposes. [snip] Oho! Thanks; I'll have to play with that. > .... > > > > P.S. -- This is my first post-by-google, so I'm interested in seeing > > how the formatting comes out. Wish me luck :-) .... > > The formatting is ok, but you should add a space after "--". Heh. That's a web form for you. It was there originally :-/ I also noticed lots of extra linefeeds in my post. I'm not sure whether I like 'em or not, as wrapping lines is a good thing but not doing it where I want gets on my nerves. Thanks again & HAND :-D -- David T-G |
|
![]() |
| Outils de la discussion | |
|
|