473,387 Members | 1,650 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,387 software developers and data experts.

arrays

I am reading the php manual online and don't understand the way arrays work.
It says arrays take a key, value pair which is fine I guess, but how do I
get an old fashion c style array? Are the keys like the indices?
say I want an array that contains the numbers 0 through 10, what do I make
the keys or does it even matter?

Thanx,
Chris
Jul 17 '05 #1
3 1805
"Christopher9" wrote:
I am reading the php manual online and don’t understand the way
arrays work.
It says arrays take a key, value pair which is fine I guess, but how do I
get an old fashion c style array? Are the keys like the indices?
say I want an array that contains the numbers 0 through 10, what do I make
the keys or does it even matter?

Thanx,
Chris


Chris, php is easy going, and at the same time, great in managing
arrays. Compared to c, most of the hard work is done for you.

So you can say
$a = array (’x’, ’y’, 33);
then $a[0] = ’x’ and $a[1] = ’y’ and $a[2] = 33
as you see, phy is just incrementing the key as it is assigning. You
can also mix types-as php is very forgiving.

Now to add another item to the array, simply do:
$a[] = 44;
in this case $a[4] = 44

you can also set the keys to be whatever you want, e.g.
$b = array( ’key1’ => 22, ’key2’ => 55);
then $b[’key1’] = 22 ...
Also php will assign incrementing keys - in addition to the defined
keys.
So $b[0] = 22 as well.

--
http://www.dbForumz.com/ This article was posted by author's request
Articles individually checked for conformance to usenet standards
Topic URL: http://www.dbForumz.com/PHP-arrays-ftopict130390.html
Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=435003
Jul 17 '05 #2
.oO(steve)
you can also set the keys to be whatever you want, e.g.
$b = array( ’key1’ => 22, ’key2’ => 55);
It's called an associative array, where the keys are strings. But in
fact there's no real difference between indexed and associative arrays,
in PHP every array is like a hashtable.
then $b[’key1’] = 22 ...
Also php will assign incrementing keys - in addition to the defined
keys.
So $b[0] = 22 as well.


$b = array('key1' => 22, 'key2' => 55);
print $b[0];

--> Notice: Undefined offset: 0 ...

$b[] = 42;
print_r($b);

--> Array
(
[key1] => 22
[key2] => 55
[0] => 42
)

Micha
Jul 17 '05 #3
"Michael Fesser" wrote:
.oO(steve)
you can also set the keys to be whatever you want, e.g.
$b = array( ’key1’ => 22, ’key2’ => 55);


It’s called an associative array, where the keys are strings.
But in
fact there’s no real difference between indexed and associative
arrays,
in PHP every array is like a hashtable.
then $b[’key1’] = 22 ...
Also php will assign incrementing keys - in addition to the definedkeys.
So $b[0] = 22 as well.


$b = array(’key1’ => 22, ’key2’ => 55);
print $b[0];

--> Notice: Undefined offset: 0 ...

$b[] = 42;
print_r($b);

--> Array
(
[key1] => 22
[key2] => 55
[0] => 42
)

Micha


Michael is correct, my mistake.
$b = array(’key1’ => 22, ’key2’ => 55);
print $b[0];

The keys assigned are ’key1’ and ’key2’ and there is no
auto-incremting of indecis starting with zero. So $b[0] IS undefined.

--
http://www.dbForumz.com/ This article was posted by author's request
Articles individually checked for conformance to usenet standards
Topic URL: http://www.dbForumz.com/PHP-arrays-ftopict130390.html
Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=435420
Jul 17 '05 #4

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

Similar topics

19
by: Canonical Latin | last post by:
"Leor Zolman" <leor@bdsoft.com> wrote > "Canonical Latin" <javaplus@hotmail.com> wrote: > > > ... > >But I'm still curious as to the rational of having type >...
21
by: Matteo Settenvini | last post by:
Ok, I'm quite a newbie, so this question may appear silly. I'm using g++ 3.3.x. I had been taught that an array isn't a lot different from a pointer (in fact you can use the pointer arithmetics to...
5
by: JezB | last post by:
What's the easiest way to concatenate arrays ? For example, I want a list of files that match one of 3 search patterns, so I need something like DirectoryInfo ld = new DirectoryInfo(searchDir);...
3
by: Michel Rouzic | last post by:
It's the first time I try using structs, and I'm getting confused with it and can't make it work properly I firstly define the structure by this : typedef struct { char *l1; int *l2; int Nval; }...
1
by: Rob Griffiths | last post by:
Can anyone explain to me the difference between an element type and a component type? In the java literature, arrays are said to have component types, whereas collections from the Collections...
41
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in...
6
by: Robert Bravery | last post by:
Hi all, Can some one show me how to achieve a cross product of arrays. So that if I had two arrays (could be any number) with three elements in each (once again could be any number) I would get:...
1
by: Doug_J_W | last post by:
I have a Visual Basic (2005) project that contains around twenty embedded text files as resources. The text files contain two columns of real numbers that are separated by tab deliminator, and are...
16
by: mike3 | last post by:
(I'm xposting this to both comp.lang.c++ and comp.os.ms- windows.programmer.win32 since there's Windows material in here as well as questions related to standard C++. Not sure how that'd go over...
29
weaknessforcats
by: weaknessforcats | last post by:
Arrays Revealed Introduction Arrays are the built-in containers of C and C++. This article assumes the reader has some experiece with arrays and array syntax but is not clear on a )exactly how...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.