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

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
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  

For example, I see the following:


And many more.

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

Thank you

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

MBS wrote:
Quoted text here. Click to load it

This is an array which uses a string as index instead of an int. This is  
called associative arrays
Quoted text here. Click to load it

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

Quoted text here. Click to load it

It is called associative array.


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

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:

// you now have an empty array
// not usually necessary as simply setting an array member will create

// Now your array has one element with an index of 0

// now your array has 2 elements, indexes 0 and 14

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

// 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.


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

Ian B wrote:
Quoted text here. Click to load it

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  

$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


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

In our last episode,  
the lovely and talented MBS  
broadcast on alt.php:

Quoted text here. Click to load it

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     
         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_

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

Quoted text here. Click to load it

 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
Andy Hassall :: :: :: disk and FTP usage analysis tool

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

MBS wrote:
Quoted text here. Click to load it

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

Site Timeline