PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > Access non-numerically indexed array by their order number
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Access non-numerically indexed array by their order number

Réponse
 
LinkBack Outils de la discussion
Vieux 26/03/2008, 23h23   #1
Mikhail Kovalev
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Access non-numerically indexed array by their order number

Hi all,

For example if I have
$array = array(
'assd' => 21,
's9aso' => 1267,
2 => 129
);

And I need to access 2nd element in the array, not the element with
the key 2, but 's9aso' => 1267.
I know array_splice works that way, for example array_slice($array, 1)
will return two last elements, ie starting at 2nd element (id=1).
Is there a way to access an array that way, by some internal id number
which always starts at 0 etc? I could use array_keys to get
array(
0 => 'assd',
1 => 's9aso',
2 => 2
)

But other than that?

Thanks!
  Réponse avec citation
Vieux 27/03/2008, 00h34   #2
Jeremy
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Access non-numerically indexed array by their order number

Mikhail Kovalev wrote:
> Hi all,
>
> For example if I have
> $array = array(
> 'assd' => 21,
> 's9aso' => 1267,
> 2 => 129
> );
>
> And I need to access 2nd element in the array, not the element with
> the key 2, but 's9aso' => 1267.
> I know array_splice works that way, for example array_slice($array, 1)
> will return two last elements, ie starting at 2nd element (id=1).
> Is there a way to access an array that way, by some internal id number
> which always starts at 0 etc? I could use array_keys to get
> array(
> 0 => 'assd',
> 1 => 's9aso',
> 2 => 2
> )
>
> But other than that?
>
> Thanks!


What's wrong with array_slice?

list($second) = array_slice($array, 1, 1, true);

A little round about, but it works.

Jeremy
  Réponse avec citation
Vieux 27/03/2008, 00h51   #3
Mikhail Kovalev
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Access non-numerically indexed array by their order number

On 27 Mar, 00:34, Jeremy <jer...@pinacol.com> wrote:
> Mikhail Kovalev wrote:
> > Hi all,

>
> > For example if I have
> > $array = array(
> > 'assd' => 21,
> > 's9aso' => 1267,
> > 2 => 129
> > );

>
> > And I need to access 2nd element in the array, not the element with
> > the key 2, but 's9aso' => 1267.
> > I know array_splice works that way, for example array_slice($array, 1)
> > will return two last elements, ie starting at 2nd element (id=1).
> > Is there a way to access an array that way, by some internal id number
> > which always starts at 0 etc? I could use array_keys to get
> > array(
> > 0 => 'assd',
> > 1 => 's9aso',
> > 2 => 2
> > )

>
> > But other than that?

>
> > Thanks!

>
> What's wrong with array_slice?
>
> list($second) = array_slice($array, 1, 1, true);
>
> A little round about, but it works.
>
> Jeremy


True, but the result is an array of length 1, not the value.
But shouldn't it be anything faster?
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 03h46.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,09645 seconds with 11 queries