|
Posted by gosha bine on 09/18/07 08:19
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
[Back to original message]
|