Array questions

Array questions

am 20.08.2007 12:43:32 von Zanna

Hi!

Some question about array.

- An array mantains always the order of item as inserted also if I use
string keys?
Or the items are "sorted" cause the hash of the key?
I hope to be clear for this question :)

- Can I know if a key is used?
Something like in_array() but for the key, not for the value.

- Can I remove an item from an array?

Thanks

Re: Array questions

am 20.08.2007 13:08:33 von kimandre

Fabio wrote:

> Hi!
>
> Some question about array.
>
> - An array mantains always the order of item as inserted also if I
> use string keys? Or the items are "sorted" cause the hash of the key?
> I hope to be clear for this question :)

Yes, the order is maintained, unless you perform some sorting on it.

If you want to sort the array based on the keys (numerical and/or
string-based), you can use ksort() for that.
http://php.net/ksort

> - Can I know if a key is used?
> Something like in_array() but for the key, not for the value.

Sure, like this:

if (isset($array["key"])) {
// do something
}

http://php.net/isset

> - Can I remove an item from an array?

Yes, just pass the array key to the unset() function, like this:

unset($array["key"]);

http://php.net/unset

If you don't know the key, but have the value, use array_search() to
find the key.

http://php.net/array_search

> Thanks

No problem.

--
Kim André Akerø
- kimandre@NOSPAMbetadome.com
(remove NOSPAM to contact me directly)

Re: Array questions

am 20.08.2007 14:10:03 von ELINTPimp

> > - Can I know if a key is used?
> > Something like in_array() but for the key, not for the value.
>
> Sure, like this:
>
> if (isset($array["key"])) {
> // do something
>
> }
>
> http://php.net/isset

array_key_exists()

Re: Array questions

am 20.08.2007 14:12:45 von colin.mckinnon

On 20 Aug, 12:08, Kim Andr=E9 Aker=F8 wrote:
> Fabio wrote:
> > Hi!
>
> > Some question about array.
>
> > - An array mantains always the order of item as inserted also if I
> > use string keys? Or the items are "sorted" cause the hash of the key?
> > I hope to be clear for this question :)
>
> Yes, the order is maintained, unless you perform some sorting on it.
>

...but relying on this being the case does not make for robust code.

If there is information in the sequence then it should be explicitly
stated in the data construct.

C

Re: Array questions

am 20.08.2007 14:15:31 von Zanna

"Kim André Akerø" ha scritto nel messaggio
news:5itb1gF3qq0jjU1@mid.individual.net...


>> Thanks
>
> No problem.

Thank yo!
Finally good news about php and someone in this NG that tell them to me :)

Another one: since the array is always ordered, can I get the item number n
also if I registered it with a key?
Something like that would work :)

$a = array("a" => "first", "b" => "second", "c" => "last one");
$value = $a[1]; // get "second" that is at the index #1


Thanks again.

Re: Array questions

am 20.08.2007 14:58:54 von zeldorblat

On Aug 20, 8:15 am, "Fabio" wrote:
> "Kim Andr=E9 Aker=F8" ha scritto nel messag=
gionews:5itb1gF3qq0jjU1@mid.individual.net...
>
> Another one: since the array is always ordered, can I get the item number=
n
> also if I registered it with a key?
> Something like that would work :)
>
> $a =3D array("a" =3D> "first", "b" =3D> "second", "c" =3D> "last one");
> $value =3D $a[1]; // get "second" that is at the index #1
>
> Thanks again.

Not directly. The key "1" isn't really any different than any other
key in the array. You can, however, get a list of the keys and then
find the position that way. So, following your example above:

$keys =3D array_keys($a);
$a[$keys[1]]; //this is "second"