473,406 Members | 2,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

MBS
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
Nov 2 '05 #1
7 1756
MBS wrote:
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.
This is an array which uses a string as index instead of an int. This is
called associative arrays
Thank you

Nov 2 '05 #2
Użytkownik "MBS" <mb*@mbs.net> napisał w wiadomo¶ci news:43***********************@authen.white.readfr eenews.net...
What is this feature called in PHP?


It is called associative array.

--
greets
T
Nov 2 '05 #3
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

Nov 2 '05 #4
In our last episode,
<43***********************@authen.white.readfreene ws.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 ei*****@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_
Nov 2 '05 #5
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

Nov 2 '05 #6
On Wed, 02 Nov 2005 05:12:34 -0600, Lars Eighner <ei*****@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
Nov 2 '05 #7

MBS wrote:
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.


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

Nov 2 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Ger | last post by:
I have not been able to find a simple, straight forward Unicode to ASCII string conversion function in VB.Net. Is that because such a function does not exists or do I overlook it? I found...
4
by: Sai | last post by:
Hi, I am using VSTO 2005 Beta for building an excel application. I added application configuration file to the project (app.config) and it has the following section in it. <configuration>...
1
by: Curtis | last post by:
I am using the application settings example @ http://msdn.microsoft.com/msdnmag/issues/05/04/AdvancedBasics/default.aspx . My application needs to save a user name and password in encrypted format...
13
by: Eric Renken | last post by:
I currently use statements like this in my 1.1 code. System.Configuration.ConfigurationSettings.AppSettings I use this in DLLs that are referenced by the application. The application has this...
3
by: יניב | last post by:
how could i write this into a sting : strXML = "<?xml version='1.0'?>\n"; thanks
3
by: Nayan Mansinha | last post by:
Hi All How can I store an array of objects in my C# Application.Settings? I have CMyObject class for which an array is created: CMyObject arr = new CMyObject; arr = new CMyObject();...
3
by: nasse | last post by:
Would you please help me in how to get the remaining column values so that I will be able to echo each value in my pages. Right now, my function is set to return just to return one field's value...
2
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
Hi Is there any way to Read an INIFile from a string or Stream instead of a physical file ??? I want to read the INIFile into a string then store in a db but when I read the string from the...
3
by: =?Utf-8?B?Sm9u?= | last post by:
Hello, I have tried to use the app.config and settings.cs files to store my data (which I want to be user changeable at runtime). I can write to (what I assume is an object in memory) and it does...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.