Re: Re: Extracting multi dimensional array value

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sun, 3 May 2015 at 12:00 German Geek <geek.de@xxxxxxxxx> wrote:

> Sorting is always more expensive than going through the array once. I would
> just iterate through once and keep track of the smallest or largest element
> by storing the smallest or largest element in a temporary variable. There
> should really be a php function for that.
>

It's not necessarily true to say that sorting is always more expensive than
going through the array once. Sorting is happening in C, whereas going
through the array once will be happening in PHP. C will definitely be
faster than PHP, but I'm not sure which will be faster between these two.

However, the more important factor is how many entries there are in the
array. Based on the example provided it's tiny, so unless the OP is doing
this operation thousands of times a second both methods are sensible
options, and sorting will contain far fewer moving parts in userland.

-Stuart

-- 
Stuart Dallas
3ft9 Ltd
http://3ft9.com/


> On Sun, 3 May 2015 4:07 am Ron Piggott <ron.piggott@xxxxxxxxxxxxxxxxxx>
> wrote:
>
> > On 02/05/15 07:29, Christoph Becker wrote:
> > > Ashley Sheridan wrote:
> > >
> > >> On 2 May 2015 11:57:42 BST, Christoph Becker <cmbecker69@xxxxxx>
> wrote:
> > >>> Ron Piggott wrote:
> > >>>
> > >>>> I have the following array:
> > >>>>
> > >>>> Array
> > >>>> (
> > >>>>      [es-PR] => 2014-11-04 08:22:07
> > >>>>      [en-US] => 2009-04-05 09:00:00
> > >>>>      [es-MX] => 2014-11-08 02:25:40
> > >>>> )
> > >>>>
> > >>>> How can I extract the oldest date from it?  The language ISO order
> is
> > >>>> going to change randomly.
> > >>> I suggest you sort() the array, and then access the first element:
> > >>>
> > >>>   sort($array);
> > >>>   $oldest = $array[0];
> > >> Sorting like this would put the earliest date first, and you can't
> > access it by numeric index.
> > Ron is looking for the oldest date, what seems to be the same as the
> > earliest date. Anyhow, otherwise one could use rsort() instead of
> > sort(). Accessing by numeric index is possible after either sorting,
> > because both functions don't keep the keys (contrary to asort() and
> > arsort()): $array = [ 'es-PR' => '2014-11-04 08:22:07', 'en-US' =>
> > '2009-04-05 09:00:00', 'es-MX' => '2014-11-08 02:25:40' ]; sort($array);
> > var_dump($array);
> >
> >
> >
> > In the end I've used
> >
> > // sort newest to oldest translation
> >
> > rsort($array);
> >
> > // extract oldest translation
> >
> > $array=end($array);
> >
> > Thank you very much for your help.
> >
> > Ron
> >
> >
> >
>

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux