get first and last items in array
get first and last items in array
am 16.11.2007 00:15:52 von mtuller
I want to get the first record and last record in an array. I don't
want any other records, but am not finding how to do it. What I am
doing is pulling out information from a database, where the sql query
would have a result much like this:
10
11
12
13
14
15
16
And I want to grab the first and last items in the array, so in this
case, 10 and 16.
Any advice would be appreciated.
Re: get first and last items in array
am 16.11.2007 00:19:06 von luiheidsgoeroe
On Fri, 16 Nov 2007 00:15:52 +0100, mtuller wrote:
> I want to get the first record and last record in an array. I don't
> want any other records, but am not finding how to do it. What I am
> doing is pulling out information from a database, where the sql query
> would have a result much like this:
>
> 10
> 11
> 12
> 13
> 14
> 15
> 16
>
> And I want to grab the first and last items in the array, so in this
> case, 10 and 16.
RTFM.....
$first =3D reset($array);
$last =3D end($array);
For ranges, look at array_slice()/array_splice()/count()
-- =
Rik Wasmus
Re: get first and last items in array
am 16.11.2007 11:00:48 von Rob
On Nov 15, 11:19 pm, "Rik Wasmus" wrote:
> On Fri, 16 Nov 2007 00:15:52 +0100, mtuller wrote:
> > I want to get the first record and last record in an array. I don't
> > want any other records, but am not finding how to do it. What I am
> > doing is pulling out information from a database, where the sql query
> > would have a result much like this:
>
> > 10
> > 11
> > 12
> > 13
> > 14
> > 15
> > 16
>
> > And I want to grab the first and last items in the array, so in this
> > case, 10 and 16.
>
> RTFM.....
>
> $first = reset($array);
> $last = end($array);
>
> For ranges, look at array_slice()/array_splice()/count()
> --
> Rik Wasmus- Hide quoted text -
>
> - Show quoted text -
I would think about changing your SQL statement to only select the
first and last record, using ORDER_BY and LIMIT. If you have a lot of
rows to load, it would be much more efficient.
Rob.
Re: get first and last items in array
am 16.11.2007 14:00:23 von Hans-Peter Sauer
Yes - how dare a user ask for php help on a php newsgroup .
> > I want to get the first record and last record in an array. I don't
> > want any other records, but am not finding how to do it. What I am
> > doing is pulling out information from a database, where the sql query
> > would have a result much like this:
> >
> > 10
> > 11
> > 12
> > 13
> > 14
> > 15
> > 16
> >
> > And I want to grab the first and last items in the array, so in this
> > case, 10 and 16.
>
> RTFM.....
>
Re: get first and last items in array
am 16.11.2007 16:02:08 von mtuller
On Nov 16, 4:00 am, Rob wrote:
> On Nov 15, 11:19 pm, "Rik Wasmus" wrote:
>
>
>
> > On Fri, 16 Nov 2007 00:15:52 +0100, mtuller wrote:
> > > I want to get the first record and last record in an array. I don't
> > > want any other records, but am not finding how to do it. What I am
> > > doing is pulling out information from a database, where the sql query
> > > would have a result much like this:
>
> > > 10
> > > 11
> > > 12
> > > 13
> > > 14
> > > 15
> > > 16
>
> > > And I want to grab the first and last items in the array, so in this
> > > case, 10 and 16.
>
> > RTFM.....
>
> > $first = reset($array);
> > $last = end($array);
>
> > For ranges, look at array_slice()/array_splice()/count()
> > --
> > Rik Wasmus- Hide quoted text -
>
> > - Show quoted text -
>
> I would think about changing your SQL statement to only select the
> first and last record, using ORDER_BY and LIMIT. If you have a lot of
> rows to load, it would be much more efficient.
>
> Rob.
Thanks. It hit me this morning that I was not looking for items in an
array, but in the rows returned. I will look into an SQL solution.
Re: get first and last items in array
am 16.11.2007 17:21:00 von luiheidsgoeroe
On Fri, 16 Nov 2007 14:00:23 +0100, Krustov wrote:
>
>
>
>
>
>> > I want to get the first record and last record in an array. I don't
>> > want any other records, but am not finding how to do it. What I am
>> > doing is pulling out information from a database, where the sql query
>> > would have a result much like this:
>> >
>> > 10
>> > 11
>> > 12
>> > 13
>> > 14
>> > 15
>> > 16
>> >
>> > And I want to grab the first and last items in the array, so in this
>> > case, 10 and 16.
>>
>> RTFM.....
...and the answer was below that line..
> Yes - how dare a user ask for php help on a php newsgroup .
What?
I stated: Read The Fine Manual, for instane, when working with arrays, one
would think to look at the array functions, .
Try to find to find the text 'first' and 'last' on that page (two terms
the op literally used), and he'd have the answer.
Stating this could be easily found in the manual, and encouraging posters
to use it, helps them more in the long run then just easy answers.
Please don't toppost.
--
Rik Wasmus