Arrays

For more information about those structures , we refer you to external literature about this broad topic .

Syntax

Specifying with array()

A key is either a nonnegative integer or a string . If a key is the standard representation of a non-negative integer , it will be interpreted as such ( i.e . '8 ' will be interpreted as 8 , while '08 ' will be interpreted as '08 ' ) .

A value can be anything .






Creating/modifying with square-bracket syntax

You can also modify an existing array , by explicitly setting values .




If $arr doesn't exist yet, it will be created. So this is also an alternative way to specify an array. To change a certain value, just assign a new value to it. If you want to remove a key/value pair, you need to unset() it.

Useful functions

There are quite some useful function for working with arrays , see the array-functions section .

הערה :

 
$a

 
=

 
array(

 
1

 
=

 
'one'

 
,

 
2

 
=

 
'two'

 
,

 
3

 
=

 
'three

 
'

 
)

 
;

 
unset

 
(

 
$a[2

 
]

 
)

 
;

 
/

 
*

 
will

 
produce

 
an

 
array

 
that

 
would

 
have

 
been

 
defined

 
as

 
$a

 
=

 
array

 
(

 
1=

 
'one'

 
,

 
3=

 
'three')

 
;

 
and

 
NOT

 
$a

 
=

 
array

 
(

 
1

 
=

 
'one'

 
,

 
2

 
=

 
'three')

 
;

 
*

 
/





The foreach control structure exists specificly for arrays .

Array do's and don'ts

Why is $foo[bar] wrong?

 
$foo[

 
bar

 
]

 
=

 
'enemy'

 
;

 
echo

 
$foo[bar]

 
;

 
/

 
/

 
etc



This is wrong, but it works. Then, why is it wrong? The reason is that, as stated in the syntax section, there must be an expression between the square brackets (' [ ' and ' ] '). That means that you can write things like this:
 
echo

 
$arr[

 
foo(true

 
)

 
]

 
;



This is an example of using a function return value as the array index. PHP knows also about constants, and you may have seen the E_* before.
 
$error_descriptions[

 
E_ERROR

 
]

 
=

 
"A

 
fatal

 
error

 
has

 
occured"

 
;

 
$error_descriptions[E_WARNING

 
]

 
=

 
"PHP

 
issued

 
a

 
warning"

 
;

 
$error_descriptions[E_NOTICE

 
]

 
=

 
"This

 
is

 
just

 
an

 
informal

 
notice"

 
;



Note that E_ERROR is also a valid identifier, just like bar in the first example. But the last example is in fact the same as writing:
 
$error_descriptions[

 
1

 
]

 
=

 
"A

 
fatal

 
error

 
has

 
occured"

 
;

 
$error_descriptions[2

 
]

 
=

 
"PHP

 
issued

 
a

 
warning"

 
;

 
$error_descriptions[8

 
]

 
=

 
"This

 
is

 
just

 
an

 
informal

 
notice"

 
;



because E_ERROR equals 1 , etc.

Then , how is it possible that $foo[ bar ] works ?

So why is it bad then?

At some point in the future , the PHP team might want to add another constant or keyword , and then you get in trouble . For example , you already cannot use the words empty and default this way , since they are special keywords .

And , if these arguments don ' t help : this syntax is simply deprecated , and it might stop working some day .

הערה : When you turn error_reporting to E_ALL , you will see that PHP generates warnings whenever this construct is used . This is also valid for other deprecated ' features' . (put the line error_reporting(E_ALL) ; in your script )

הערה : Inside a double-quoted string , an other syntax is valid . See variable parsing in strings for more details .

Examples

 
/

 
/

 
this

 
$a

 
=

 
array(

 
'color

 
'

 
=

 
'red

 
'

 
,

 
'taste

 
'

 
=

 
'sweet

 
'

 
,

 
'shape

 
'

 
=

 
'round

 
'

 
,

 
'name

 
'

 
=

 
'apple

 
'

 
,

 
4

 
/

 
/

 
key

 
will

 
be

 
0

 
)

 
;

 
/

 
/

 
is

 
completely

 
equivalent

 
with

 
$a['color'

 
]

 
=

 
'red'

 
;

 
$a['taste'

 
]

 
=

 
'sweet'

 
;

 
$a['shape'

 
]

 
=

 
'round'

 
;

 
$a['name'

 
]

 
=

 
'apple'

 
;

 
$a[

 
]

 
=

 
4

 
;

 
/

 
/

 
key

 
will

 
be

 
0

 
$b[

 
]

 
=

 
'a'

 
;

 
$b[

 
]

 
=

 
'b'

 
;

 
$b[

 
]

 
=

 
'c'

 
;

 
/

 
/

 
will

 
result

 
in

 
the

 
array

 
array

 
(

 
0

 
=

 
'a

 
'

 
,

 
1

 
=

 
'b

 
'

 
,

 
2

 
=

 
'c

 
'

 
)

 
,

 
/

 
/

 
or

 
simply

 
array('a'

 
,

 
'b'

 
,

 
'c'

 
)





דוגמה 6-4 .

 
/

 
/

 
Array

 
as

 
(

 
property

 
-)map

 
$map

 
=

 
array

 
(

 
'version

 
'

 
=

 
4

 
,

 
'OS

 
'

 
=

 
'Linux

 
'

 
,

 
'lang

 
'

 
=

 
'english

 
'

 
,

 
'short_tags

 
'

 
=

 
true

 
)

 
;

 
/

 
/

 
strictly

 
numerical

 
keys

 
$array

 
=

 
array

 
(

 
7

 
,

 
8

 
,

 
0

 
,

 
156

 
,

 
-10

 
)

 
;

 
/

 
/

 
this

 
is

 
the

 
same

 
as

 
array

 
(

 
0

 
=

 
7

 
,

 
1

 
=

 
8

 
,

 
..

 
.

 
)

 
$switching

 
=

 
array

 
(

 
10

 
/

 
/

 
key

 
=

 
0

 
,

 
5

 
=

 
6

 
,

 
3

 
=

 
7

 
,

 
'a

 
'

 
=

 
4

 
,

 
11

 
/

 
/

 
key

 
=

 
6

 
(maximum

 
of

 
integer-indices

 
was

 
5

 
)

 
,

 
'8

 
'

 
=

 
2

 
/

 
/

 
key

 
=

 
8

 
(integer

 
!

 
)

 
,

 
'02

 
'

 
=

 
77

 
/

 
/

 
key

 
=

 
'02

 
'

 
,

 
0

 
=

 
12

 
/

 
/

 
the

 
value

 
10

 
will

 
be

 
overwritten

 
by

 
12

 
)

 
;

 
/

 
/

 
empty

 
array

 
$empty

 
=

 
array()

 
;



דוגמה 6-5 .

 
$colors

 
=

 
array(

 
'red'

 
,'blue'

 
,'green'

 
,'yellow')

 
;

 
foreach

 
(

 
$colors

 
as

 
$color

 
)

 
{

 
echo

 
"Do

 
you

 
like

 
$color?\n"

 
;

 
}

 
/

 
*

 
output

 
:
















 
Do

 
you

 
like

 
yellow

 
?

 
*

 
/



דוגמה 6-6 .

 
foreach

 
(

 
$colors

 
as

 
$key

 
=

 
$color

 
)

 
{

 
/

 
/

 
won't

 
work

 
:

 
/

 
/$color

 
=

 
strtoupper($color)

 
;

 
/

 
/works

 
:

 
$colors[$key

 
]

 
=

 
strtoupper($color)

 
;

 
}

 
print_r($colors)

 
;

 
/

 
*

 
output

 
:




 
Array

 
(

 
[0

 
]

 
=

 
RED

 
[1

 
]

 
=

 
BLUE

 
[2

 
]

 
=

 
GREEN

 
[3

 
]

 
=

 
YELLOW

 
)

 
*

 
/





דוגמה 6-7 .

 
$firstquarter

 
=

 
array(

 
1

 
=

 
'January'

 
,

 
'February'

 
,

 
'March')

 
;

 
print_r($firstquarter)

 
;

 
/

 
*

 
output

 
:









דוגמה 6-8 . Filling real array

 
/

 
/

 
fill

 
an

 
array

 
with

 
all

 
items

 
from

 
a

 
directory

 
$handle

 
=

 
opendir(

 
'

 
.')

 
;

 
while

 
($file

 
=

 
readdir($handle)

 
)

 
{

 
$files[

 
]

 
=

 
$file

 
;

 
}

 
closedir($handle)

 
;



See array-functions for more information .

דוגמה 6-9 .

 
sort(

 
$files)

 
;

 
print_r($files)

 
;



דוגמה 6-10 .

 
$fruits

 
=

 
array

 
(

 
"fruits

 
"

 
=

 
array

 
(

 
"a

 
"

 
=

 
"orange

 
"

 
,

 
"b

 
"

 
=

 
"banana

 
"

 
,

 
"c

 
"

 
=

 
"apple

 
"

 
                                     )

 
,

 
"numbers

 
"

 
=

 
array

 
(

 
1

 
,

 
2

 
,

 
3

 
,

 
4

 
,

 
5

 
,

 
6

 
                                     )

 
,

 
"holes

 
"

 
=

 
array

 
(

 
"first

 
"

 
,

 
5

 
=

 
"second

 
"

 
,

 
"third

 
"

 
                                     )

 
)

 
;