Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Is $Settings['string'] An Example Of An Array?

0 views
Skip to first unread message

MBS

unread,
Nov 2, 2005, 1:15:47 AM11/2/05
to
Greetings. I'm still pretty new to PHP and I have a question. I know that
variables are preceeded by a "$" (dollar sign). Typically, a variable has
one value, unless it is an array. Then it is essentially a pointer to
numerous values sequential in memory. The code I'm looking at now is using
the same variable name, but assigning a different "index" to it, if you
will.

For example, I see the following:

$settings['use_default_images']
$settings['doctype']
$settings['theme_url']

And many more.

What is this feature called in PHP? I looked at the reference on php.net
and it isn't anywhere to be found under arrays.

Thank you

Siv Hansen

unread,
Nov 2, 2005, 2:00:06 AM11/2/05
to

This is an array which uses a string as index instead of an int. This is
called associative arrays
>
> Thank you

Tadeusz S.

unread,
Nov 2, 2005, 2:01:07 AM11/2/05
to
Użytkownik "MBS" <m...@mbs.net> napisał w wiadomości news:43685993$0$93694$892e...@authen.white.readfreenews.net...

> What is this feature called in PHP?

It is called associative array.

--
greets
T

Ian B

unread,
Nov 2, 2005, 4:58:18 AM11/2/05
to
Actually, there is only one type of array in PHP and it is really an
ordered map.

PHP maps keys to values and indexes to values.

PHP arrays can appear to behave a little weird if you are used to
'normal' arrays, but they are very flexible once you get the hang of
them. Here are a few things to watch out for:

$stuff=array();
// you now have an empty array
// not usually necessary as simply setting an array member will create
it

$stuff[]="Fred";
// Now your array has one element with an index of 0

$stuff[14]="Bert";
// now your array has 2 elements, indexes 0 and 14

$stuff[] = "Anne";
// 0, 14, 15

$stuff["phone"]="0123456789";
// 0, 14, 15, 16
// but $stuff[16] get you the same thing as $stuff['phone']

// note that there is no $stuff[3] for example.
// trying to use it will cause an error
// also, deleting $stuff[15] will leave you with 0, 14, 16 - the gaps
don't close up
// have a play around - var_dump($stuff) will show you what you have in
the array.

Ian

Lars Eighner

unread,
Nov 2, 2005, 6:12:34 AM11/2/05
to
In our last episode,
<43685993$0$93694$892e...@authen.white.readfreenews.net>,
the lovely and talented MBS
broadcast on alt.php:

> Greetings. I'm still pretty new to PHP and I have a question. I know that
> variables are preceeded by a "$" (dollar sign). Typically, a variable has
> one value, unless it is an array. Then it is essentially a pointer to
> numerous values sequential in memory. The code I'm looking at now is using
> the same variable name, but assigning a different "index" to it, if you
> will.

> For example, I see the following:

> $settings['use_default_images']
> $settings['doctype']
> $settings['theme_url']

> And many more.

> What is this feature called in PHP?

Pretty much the same thing it is called in Perl or any number
of other language. It is a hash (or associative array, if you
like big words or you are looking them up in an index).

I'm pretty sure you are used to arrays with number indices:

$a[0], $bugbear[15], $pinky[$i] where $i is an integer.

Associative arrays are very similar, but use strings as
indices. You have offered some examples, and $settings[$str]
is another where $str is a string. The strings used as indices
are called "keys" and they are *associated* with "values."

Now you probably have noticed, integers have a natural order.
But strings don't. So you have to be very careful if you ever
think of trying to do something like sort an associate array or
try to form and idea of "first," "last," or "next" in relation
to an associative array. There is no general guarantee of order
in an associate array.

Naturally, it seldom makes any sense to try to do arithmetic
on hash keys. In other words, you may be used to the idea
that if $a[$n] is a value in an array $a[$n+1] is the "next"
value (if there is one). But if you $jimmy['age'], then
$jimmy['age' + 1] is most likely nonsense. You can do
string operations on keys, although you may or may not ever
find a situation in which it is useful to do so (i.e.
$jimmy['ag' . 'e'] = $jimmy['age']). In particular,
if first you do $jimmy['age'] = 24; and then
$jimmy['apt'] = 1191; $jimmy['age' + 1] in general is not
equal to $jimmy['apt'].

Associative arrays are very good for operations with things
like databases and other sets of related data. It is much
easier to deal with $jimmy['age'] than to try to remember
whether age is $jimmy[5] or $jimmy[6]. Generally you don't care
whether Jimmy's age or his apartment number comes first in the
record, you just want to be sure you don't get them confused.

--
Lars Eighner eig...@io.com http://www.larseighner.com/
I don't see posts from or threads started from googlegroups.
War on Terrorism: Treat Readers like Mushrooms
"DO NOT USE photos on Page 1A showing civilian casualties from the U.S. war
on Afghanistan." -Memo, _Panama City_ (FL) _News Herald_

Janwillem Borleffs

unread,
Nov 2, 2005, 3:28:02 PM11/2/05
to
Ian B wrote:
> $stuff["phone"]="0123456789";
> // 0, 14, 15, 16
> // but $stuff[16] get you the same thing as $stuff['phone']
>

No, it won't. Index 16 will not be defined unless specified. If you want
$stuff[16] to return the same value as the 'phone' key, you should define
both:

$stuff['phone'] = 12345;
$stuff[16] =& $stuff['phone'];
$stuff['phone'] =& $stuff[16];

$stuff['phone'] .= 6;
$stuff[16] .= 7;
print $stuff[16]; // 1234567
print $stuff['phone']; // 1234567


JW

Andy Hassall

unread,
Nov 2, 2005, 3:47:32 PM11/2/05
to
On Wed, 02 Nov 2005 05:12:34 -0600, Lars Eighner <eig...@io.com> wrote:

>Now you probably have noticed, integers have a natural order.
>But strings don't. So you have to be very careful if you ever
>think of trying to do something like sort an associate array or
>try to form and idea of "first," "last," or "next" in relation
>to an associative array. There is no general guarantee of order
>in an associate array.

PHP differs from Perl here, in that the key ordering is preserved, in the
order that the keys were assigned (or you can modify the ordering e.g. with
ksort).
--
Andy Hassall :: an...@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

Chung Leong

unread,
Nov 2, 2005, 5:26:35 PM11/2/05
to

Arrays with string indices are called associative arrays or hash
tables. The term in VB-speak I believe is dictionary.

0 new messages