Can I make an array of tuples? Is there a better way?
Can I make an array of tuples? Is there a better way?
am 15.08.2007 03:16:45 von ben.carbery
Pretty new to php so sorry if this is an obvious question.
I want to create an array that contains a tuple, i.e. a pair of
values.
e.g. myarray[$key] = ($firstname,$lastname)
I would then need a function that returns only the first or second
member of the tuple.
The tuple could also be a list, provided there's a function for only
returning the value at a particular index value of the list.
I am sure there is an obvious way to accomplish the same thing in php
(without using a multi-dimensional array) but it is escaping me at the
moment.
Ben
Re: Can I make an array of tuples? Is there a better way?
am 15.08.2007 03:25:28 von Jerry Stuckle
ben@spam.com wrote:
> Pretty new to php so sorry if this is an obvious question.
>
> I want to create an array that contains a tuple, i.e. a pair of
> values.
>
> e.g. myarray[$key] = ($firstname,$lastname)
>
> I would then need a function that returns only the first or second
> member of the tuple.
>
> The tuple could also be a list, provided there's a function for only
> returning the value at a particular index value of the list.
>
> I am sure there is an obvious way to accomplish the same thing in php
> (without using a multi-dimensional array) but it is escaping me at the
> moment.
>
> Ben
>
Two choices:
1. Multidimensional array
2. An array of objects
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Re: Can I make an array of tuples? Is there a better way?
am 15.08.2007 04:33:46 von musiccomposition
ben@spam.com wrote:
> Pretty new to php so sorry if this is an obvious question.
>
> I want to create an array that contains a tuple, i.e. a pair of
> values.
>
> e.g. myarray[$key] = ($firstname,$lastname)
>
> I would then need a function that returns only the first or second
> member of the tuple.
>
> The tuple could also be a list, provided there's a function for only
> returning the value at a particular index value of the list.
>
> I am sure there is an obvious way to accomplish the same thing in php
> (without using a multi-dimensional array) but it is escaping me at the
> moment.
No, (if only we had Python) you have to use a multi-dimensional array:
myarray[$key] = array($firstname, $lastname);
function getFirstName(name) {
return name[0];
}
function getLastName(name) {
return name[1];
}
>
> Ben
Re: Can I make an array of tuples? Is there a better way?
am 15.08.2007 10:08:34 von Toby A Inkster
ben@spam.com wrote:
> I want to create an array that contains a tuple, i.e. a pair of
> values.
PHP doesn't have a tuple data type: arrays are typically used instead. So
an array of tuples would be represented as an array or arrays.
I get the impression that you're trying to avoid an array of arrays. Why
is that?
--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.12-12mdksmp, up 55 days, 11:47.]
Fake Steve is Dead; Long Live Fake Bob!
http://tobyinkster.co.uk/blog/2007/08/13/fake-bob/