please note that when arrays are copied, the "reference status" of their members is preserved (http://www.php.net/manual/en/language.references.whatdo.php).ΠΠ°ΡΠΈΠ² Π² PHP - ΡΠ΅ ΡΠΏΠΎΡΡΠ΄ΠΊΠΎΠ²Π°Π½Π° ΡΡΡΡΠΊΡΡΡΠ° Π΄Π°Π½ΠΈΡ , ΡΠΊΠ° Π·Π²'ΡΠ·ΡΡ Π·Π½Π°ΡΠ΅Π½Π½Ρ Ρ ΠΊΠ»ΡΡΡ. Π¦Π΅ΠΉ ΡΠΈΠΏ ΠΎΠΏΡΠΈΠΌΡΠ·ΠΎΠ²Π°Π½ΠΈΠΉ Π΄Π»Ρ ΡΡΠ·Π½ΠΈΡ ΡΡΠ»Π΅ΠΉ, ΡΠ· Π½ΠΈΠΌ ΠΌΠΎΠΆΠ½Π° ΠΏΡΠ°ΡΡΠ²Π°ΡΠΈ ΡΠΊ ΡΠ· ΠΌΠ°ΡΠΈΠ²ΠΎΠΌ, ΡΠΏΠΈΡΠΊΠΎΠΌ (Π²Π΅ΠΊΡΠΎΡΠΎΠΌ), Ρ Π΅Ρ-ΡΠ°Π±Π»ΠΈΡΠ΅Ρ (ΡΠ΅Π°Π»ΡΠ·Π°ΡΡΡ ΠΊΠ°ΡΡΠΈ), ΡΠ»ΠΎΠ²Π½ΠΈΠΊΠΎΠΌ, ΠΊΠΎΠ»Π΅ΠΊΡΡΡΡ, ΡΡΠ΅ΠΊΠΎΠΌ, ΡΠ΅ΡΠ³ΠΎΡ, Ρ, ΠΌΠ°Π±ΡΡΡ, Π±ΡΠ»ΡΡΠ΅. ΠΡΠΊΡΠ»ΡΠΊΠΈ Π·Π½Π°ΡΠ΅Π½Π½Ρ ΠΌΠ°ΡΠΈΠ²Ρ ΠΌΠΎΠΆΡΡΡ Π±ΡΡΠΈ ΡΠ½ΡΠΈΠΌΠΈ ΠΌΠ°ΡΠΈΠ²Π°ΠΌΠΈ, ΡΠ°ΠΊΠΎΠΆ ΠΌΠΎΠΆΠ»ΠΈΠ²Ρ Π΄Π΅ΡΠ΅Π²Π° ΡΠ° Π±Π°Π³Π°ΡΠΎΠ²ΠΈΠΌΡΡΠ½Ρ ΠΌΠ°ΡΠΈΠ²ΠΈ.
ΠΠΎΡΡΠ½Π΅Π½Π½Ρ ΡΠΈΡ ΡΡΡΡΠΊΡΡΡ Π΄Π°Π½ΠΈΡ Π²ΠΈΡ ΠΎΠ΄ΠΈΡΡ Π·Π° ΡΠ°ΠΌΠΊΠΈ ΡΡΠΎΠ³ΠΎ ΠΏΠΎΡΡΠ±Π½ΠΈΠΊΠ°, Π°Π»Π΅ Π΄Π»Ρ ΠΊΠΎΠΆΠ½ΠΎΡ Π· Π½ΠΈΡ Π½Π°Π΄Π°ΡΡΡΡΡ ΠΏΡΠΈΠ½Π°ΠΉΠΌΠ½Ρ ΠΎΠ΄ΠΈΠ½ ΠΏΡΠΈΠΊΠ»Π°Π΄. Π©ΠΎΠ± ΠΎΡΡΠΈΠΌΠ°ΡΠΈ Π΄ΠΎΠ΄Π°ΡΠΊΠΎΠ²Ρ ΡΠ½ΡΠΎΡΠΌΠ°ΡΡΡ, ΠΏΠ΅ΡΠ΅Π³Π»ΡΠ½ΡΡΠ΅ Π·Π½Π°ΡΠ½Ρ ΠΊΡΠ»ΡΠΊΡΡΡΡ Π»ΡΡΠ΅ΡΠ°ΡΡΡΠΈ, ΡΠΊΠ° ΡΡΠ½ΡΡ Π½Π° ΡΡ ΡΠΈΡΠΎΠΊΡ ΡΠ΅ΠΌΡ.
ΠΠ°ΡΠΈΠ² (array) ΠΌΠΎΠΆΠ½Π° ΡΡΠ²ΠΎΡΠΈΡΠΈ Π·Π° Π΄ΠΎΠΏΠΎΠΌΠΎΠ³ΠΎΡ ΠΌΠΎΠ²Π½ΠΎΡ ΠΊΠΎΠ½ΡΡΡΡΠΊΡΡΡ
array(). ΠΡΠ½ ΠΏΡΠΈΠΉΠΌΠ°Ρ Π±ΡΠ΄Ρ-ΡΠΊΡ ΠΊΡΠ»ΡΠΊΡΡΡΡ ΡΠΎΠ·Π΄ΡΠ»Π΅Π½ΠΈΡ
ΠΊΠΎΠΌΠ°ΠΌΠΈ
ΠΊΠ»ΡΡ => Π·Π½Π°ΡΠ΅Π½Π½Ρ
ΠΏΠ°Ρ ΡΠΊ ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡΠΈ.
array(
key => value,
key2 => value2,
key3 => value3,
...
)
ΠΠΎΠΌΠ° ΠΏΡΡΠ»Ρ ΠΎΡΡΠ°Π½Π½ΡΠΎΠ³ΠΎ Π΅Π»Π΅ΠΌΠ΅Π½ΡΠ° ΠΌΠ°ΡΠΈΠ²Ρ Π½Π΅ΠΎΠ±ΠΎΠ²βΡΠ·ΠΊΠΎΠ²Π°, ΡΡ ΠΌΠΎΠΆΠ½Π° ΠΎΠΏΡΡΡΠΈΡΠΈ. ΠΠ°Π·Π²ΠΈΡΠ°ΠΉ ΡΠ΅ ΡΠΎΠ±ΠΈΡΡΡΡ
Π΄Π»Ρ ΠΎΠ΄Π½ΠΎΡΡΠ΄ΠΊΠΎΠ²ΠΈΡ
ΠΌΠ°ΡΠΈΠ²ΡΠ², Π½Π°ΠΏΡΠΈΠΊΠ»Π°Π΄ array(1, 2) Π²Π²Π°ΠΆΠ°ΡΡΡΡΡ ΠΊΡΠ°ΡΠΈΠΌ, Π½ΡΠΆ
array(1, 2, ). Π ΡΠ½ΡΠΎΠ³ΠΎ Π±ΠΎΠΊΡ, Π΄Π»Ρ Π±Π°Π³Π°ΡΠΎΡΡΠ΄ΠΊΠΎΠ²ΠΈΡ
ΠΌΠ°ΡΠΈΠ²ΡΠ² ΠΊΡΠ½ΡΠ΅Π²Π° ΠΊΠΎΠΌΠ°
Π·Π°Π·Π²ΠΈΡΠ°ΠΉ Π²ΠΈΠΊΠΎΡΠΈΡΡΠΎΠ²ΡΡΡΡΡΡ, ΠΎΡΠΊΡΠ»ΡΠΊΠΈ Π΄ΠΎΠ·Π²ΠΎΠ»ΡΡ Π»Π΅Π³ΡΠ΅ Π΄ΠΎΠ΄Π°Π²Π°ΡΠΈ Π½ΠΎΠ²Ρ Π΅Π»Π΅ΠΌΠ΅Π½ΡΠΈ Π² ΠΊΡΠ½Π΅ΡΡ.
ΠΠ°ΡΠ²Π°ΠΆΠ΅Π½Π½Ρ:
ΠΡΠ½ΡΡ ΠΊΠΎΡΠΎΡΠΊΠΈΠΉ ΡΠΈΠ½ΡΠ°ΠΊΡΠΈΡ ΠΌΠ°ΡΠΈΠ²Ρ, ΡΠΊΠΈΠΉ Π·Π°ΠΌΡΠ½ΡΡ
array()Π½Π°[].
ΠΡΠΈΠΊΠ»Π°Π΄ #1 ΠΡΠΎΡΡΠΈΠΉ ΠΌΠ°ΡΠΈΠ²
<?php
$array = array(
"foo" => "bar",
"bar" => "foo",
);
// ΠΠΈΠΊΠΎΡΠΈΡΡΠ°Π½Π½Ρ ΠΊΠΎΡΠΎΡΠΊΠΎΠ³ΠΎ ΡΠΈΠ½ΡΠ°ΠΊΡΠΈΡΡ ΠΌΠ°ΡΠΈΠ²Ρ
$array = [
"foo" => "bar",
"bar" => "foo",
];
?>ΠΠ»ΡΡ ΠΌΠ°ΡΠΈΠ²Ρ (key) ΠΌΠΎΠΆΠ΅ Π±ΡΡΠΈ Π·Π°Π΄Π°Π½ΠΈΠΉ ΡΡΠ»ΠΈΠΌ ΡΠΈΡΠ»ΠΎΠΌ (int) Π°Π±ΠΎ ΡΡΡΠΎΠΊΠΎΡ (string). ΠΠ½Π°ΡΠ΅Π½Π½Ρ ΠΌΠ°ΡΠΈΠ²Ρ (value) ΠΌΠΎΠΆΠ΅ Π±ΡΡΠΈ Π±ΡΠ΄Ρ-ΡΠΊΠΈΠΌ ΡΠΈΠΏΠΎΠΌ.
ΠΡΡΠΌ ΡΠΎΠ³ΠΎ, Π²ΡΠ΄Π±ΡΠ²Π°ΡΠΈΠΌΡΡΡΡΡ Π½Π°ΡΡΡΠΏΠ½Ρ ΠΏΡΠΈΠ²Π΅Π΄Π΅Π½Π½Ρ ΠΊΠ»ΡΡΡΠ² key:
+), Π±ΡΠ΄ΡΡΡ ΠΏΡΠΈΠ²Π΅Π΄Π΅Π½Ρ
Π΄ΠΎ ΡΠΈΠΏΡ int. ΠΠ°ΠΏΡΠΈΠΊΠ»Π°Π΄ ΠΊΠ»ΡΡ "8" ΡΠ°ΠΊΡΠΈΡΠ½ΠΎ Π·Π±Π΅ΡΡΠ³Π°ΡΠΈΠΌΠ΅ΡΡΡΡ
ΠΏΡΠ΄ 8. Π ΡΠ½ΡΠΎΠ³ΠΎ Π±ΠΎΠΊΡ, "08" Π½Π΅ Π±ΡΠ΄Π΅ ΠΏΡΠΈΠ²Π΅Π΄Π΅Π½ΠΎ,
ΠΎΡΠΊΡΠ»ΡΠΊΠΈ ΡΠ΅ Π½Π΅Π΄ΡΠΉΡΠ½Π΅ Π΄Π΅ΡΡΡΠΊΠΎΠ²Π΅ ΡΡΠ»Π΅ ΡΠΈΡΠ»ΠΎ.
8.7 ΡΠ°ΠΊΡΠΈΡΠ½ΠΎ
Π·Π±Π΅ΡΡΠ³Π°ΡΠΈΠΌΠ΅ΡΡΡΡ ΠΏΡΠ΄ 8.
true ΡΠ°ΠΊΡΠΈΡΠ½ΠΎ Π·Π±Π΅ΡΡΠ³Π°ΡΠΈΠΌΠ΅ΡΡΡΡ ΠΏΡΠ΄ 1, Π° ΠΊΠ»ΡΡ false ΠΏΡΠ΄ 0.
null ΡΠ°ΠΊΡΠΈΡΠ½ΠΎ Π·Π±Π΅ΡΡΠ³Π°ΡΠΈΠΌΠ΅ΡΡΡΡ ΠΏΡΠ΄ "".
Illegal offset type.
Π―ΠΊΡΠΎ ΠΊΡΠ»ΡΠΊΠ° Π΅Π»Π΅ΠΌΠ΅Π½ΡΡΠ² Π² ΠΎΠ³ΠΎΠ»ΠΎΡΠ΅Π½Π½Ρ ΠΌΠ°ΡΠΈΠ²Ρ Π²ΠΈΠΊΠΎΡΠΈΡΡΠΎΠ²ΡΡΡΡ ΡΠΎΠΉ ΡΠ°ΠΌΠΈΠΉ ΠΊΠ»ΡΡ, Π±ΡΠ΄Π΅ Π²ΠΈΠΊΠΎΡΠΈΡΡΠ°Π½ΠΎ Π»ΠΈΡΠ΅ ΠΎΡΡΠ°Π½Π½ΡΠΉ, ΠΎΡΠΊΡΠ»ΡΠΊΠΈ Π²ΡΡ ΡΠ½ΡΡ Π±ΡΠ΄Π΅ ΠΏΠ΅ΡΠ΅Π·Π°ΠΏΠΈΡΠ°Π½ΠΎ.
ΠΡΠΈΠΊΠ»Π°Π΄ #2 ΠΡΠΈΠΊΠ»Π°Π΄ ΠΏΠ΅ΡΠ΅Π²Π΅Π΄Π΅Π½Π½Ρ ΡΠΈΠΏΡΠ² Ρ ΠΏΠ΅ΡΠ΅Π·Π°ΠΏΠΈΡΡ
<?php
$array = array(
1 => "a",
"1" => "b",
1.5 => "c",
true => "d",
);
var_dump($array);
?>ΠΠΎΠ΄Π°Π½ΠΈΠΉ Π²ΠΈΡΠ΅ ΠΏΡΠΈΠΊΠ»Π°Π΄ Π²ΠΈΠ²Π΅Π΄Π΅:
array(1) {
[1]=>
string(1) "d"
}
ΠΡΠΊΡΠ»ΡΠΊΠΈ Π²ΡΡ ΠΊΠ»ΡΡΡ Ρ Π½Π°Π²Π΅Π΄Π΅Π½ΠΎΠΌΡ Π²ΠΈΡΠ΅ ΠΏΡΠΈΠΊΠ»Π°Π΄Ρ ΠΏΡΠΈΠ²Π΅Π΄Π΅Π½ΠΎ Π΄ΠΎ 1,
Π·Π½Π°ΡΠ΅Π½Π½Ρ Π±ΡΠ΄Π΅ ΠΏΠ΅ΡΠ΅Π·Π°ΠΏΠΈΡΠ°Π½ΠΎ Π½Π° ΠΊΠΎΠΆΠ½ΠΎΠΌΡ Π½ΠΎΠ²ΠΎΠΌΡ Π΅Π»Π΅ΠΌΠ΅Π½ΡΡ, ΡΠ° Π·Π°Π»ΠΈΡΠΈΡΡΡΡ ΡΡΠ»ΡΠΊΠΈ
ΠΎΡΡΠ°Π½Π½Ρ ΠΏΡΠΈΡΠ²ΠΎΡΠ½Π΅ Π·Π½Π°ΡΠ΅Π½Π½Ρ "d".
ΠΠ°ΡΠΈΠ²ΠΈ PHP ΠΌΠΎΠΆΡΡΡ ΠΌΡΡΡΠΈΡΠΈ ΡΡΠ»ΠΎΡΠΈΡΠ»Π΅Π½Π½Ρ (int) Ρ ΡΡΡΠΎΠΊΠΎΠ²Ρ (string) ΠΊΠ»ΡΡΡ ΠΎΠ΄Π½ΠΎΡΠ°ΡΠ½ΠΎ, ΠΎΡΠΊΡΠ»ΡΠΊΠΈ PHP Π½Π΅ ΡΠΎΠ·ΡΡΠ·Π½ΡΡ ΡΠ½Π΄Π΅ΠΊΡΠΎΠ²Π°Π½Ρ ΡΠ° Π°ΡΠΎΡΡΠ°ΡΠΈΠ²Π½Ρ ΠΌΠ°ΡΠΈΠ²ΠΈ.
ΠΡΠΈΠΊΠ»Π°Π΄ #3 ΠΠΌΡΡΠ°Π½Ρ ΡΡΠ»ΠΎΡΠΈΡΠ»Π΅Π½Π½Ρ (int) Ρ ΡΡΡΠΎΠΊΠΎΠ²Ρ (string) ΠΊΠ»ΡΡΡ
<?php
$array = array(
"foo" => "bar",
"bar" => "foo",
100 => -100,
-100 => 100,
);
var_dump($array);
?>ΠΠΎΠ΄Π°Π½ΠΈΠΉ Π²ΠΈΡΠ΅ ΠΏΡΠΈΠΊΠ»Π°Π΄ Π²ΠΈΠ²Π΅Π΄Π΅:
array(4) {
["foo"]=>
string(3) "bar"
["bar"]=>
string(3) "foo"
[100]=>
int(-100)
[-100]=>
int(100)
}
ΠΠ»ΡΡ (key) Π½Π΅ΠΎΠ±ΠΎΠ²'ΡΠ·ΠΊΠΎΠ²ΠΈΠΉ. Π―ΠΊΡΠΎ ΠΉΠΎΠ³ΠΎ Π½Π΅ Π²ΠΊΠ°Π·Π°Π½ΠΎ, PHP Π²ΠΈΠΊΠΎΡΠΈΡΡΠΎΠ²ΡΠ²Π°ΡΠΈΠΌΠ΅ ΠΏΡΠΈΡΡΡΡ Π½Π°ΠΉΠ±ΡΠ»ΡΡΠΎΠ³ΠΎ ΡΠ°Π½ΡΡΠ΅ Π²ΠΈΠΊΠΎΡΠΈΡΡΠ°Π½ΠΎΠ³ΠΎ ΡΡΠ»ΠΎΠ³ΠΎ (int) ΠΊΠ»ΡΡΠ°.
ΠΡΠΈΠΊΠ»Π°Π΄ #4 ΠΠ½Π΄Π΅ΠΊΡΠΎΠ²Π°Π½Ρ ΠΌΠ°ΡΠΈΠ²ΠΈ Π±Π΅Π· ΠΊΠ»ΡΡΠ°
<?php
$array = array("foo", "bar", "hello", "world");
var_dump($array);
?>ΠΠΎΠ΄Π°Π½ΠΈΠΉ Π²ΠΈΡΠ΅ ΠΏΡΠΈΠΊΠ»Π°Π΄ Π²ΠΈΠ²Π΅Π΄Π΅:
array(4) {
[0]=>
string(3) "foo"
[1]=>
string(3) "bar"
[2]=>
string(5) "hello"
[3]=>
string(5) "world"
}
ΠΠΎΠΆΠ½Π° Π²ΠΊΠ°Π·Π°ΡΠΈ ΠΊΠ»ΡΡ Π΄Π»Ρ ΠΎΠ΄Π½ΠΈΡ Π΅Π»Π΅ΠΌΠ΅Π½ΡΡΠ² Ρ Π½Π΅ Π²ΠΊΠ°Π·Π°ΡΠΈ Π΄Π»Ρ ΡΠ½ΡΠΈΡ :
ΠΡΠΈΠΊΠ»Π°Π΄ #5 ΠΠ»ΡΡΡ Π½Π΅ Π½Π° Π²ΡΡΡ Π΅Π»Π΅ΠΌΠ΅Π½ΡΠ°Ρ
<?php
$array = array(
"a",
"b",
6 => "c",
"d",
);
var_dump($array);
?>ΠΠΎΠ΄Π°Π½ΠΈΠΉ Π²ΠΈΡΠ΅ ΠΏΡΠΈΠΊΠ»Π°Π΄ Π²ΠΈΠ²Π΅Π΄Π΅:
array(4) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[6]=>
string(1) "c"
[7]=>
string(1) "d"
}
Π―ΠΊ Π²ΠΈ Π±Π°ΡΠΈΡΠ΅, ΠΎΡΡΠ°Π½Π½ΡΠΎΠΌΡ Π·Π½Π°ΡΠ΅Π½Π½Ρ "d" Π±ΡΠ»ΠΎ ΠΏΡΠΈΡΠ²ΠΎΡΠ½ΠΎ
ΠΊΠ»ΡΡ 7. Π¦Π΅ ΡΠΎΠΌΡ, ΡΠΎ Π½Π°ΠΉΠ±ΡΠ»ΡΡΠΈΠΉ ΡΡΠ»ΠΈΠΉ ΠΊΠ»ΡΡ Π΄ΠΎ ΡΡΠΎΠ³ΠΎ
Π±ΡΠ² 6.
ΠΡΠΈΠΊΠ»Π°Π΄ #6 ΠΡΠΈΠΊΠ»Π°Π΄ ΡΠΊΠ»Π°Π΄Π½ΠΎΠ³ΠΎ ΠΏΡΠΈΠ²Π΅Π΄Π΅Π½Π½Ρ ΡΠΈΠΏΡ ΡΠ° ΠΏΠ΅ΡΠ΅Π·Π°ΠΏΠΈΡΡ
Π¦Π΅ΠΉ ΠΏΡΠΈΠΊΠ»Π°Π΄ Π²ΠΊΠ»ΡΡΠ°Ρ Π²ΡΡ Π²Π°ΡΡΠ°Π½ΡΠΈ ΠΏΡΠΈΠ²Π΅Π΄Π΅Π½Π½Ρ ΡΠΈΠΏΡΠ² ΠΊΠ»ΡΡΡΠ² Ρ ΠΏΠ΅ΡΠ΅Π·Π°ΠΏΠΈΡΡ Π΅Π»Π΅ΠΌΠ΅Π½ΡΡΠ².
<?php
$array = array(
1 => 'a',
'1' => 'b', // Π·Π½Π°ΡΠ΅Π½Π½Ρ "a" Π±ΡΠ΄Π΅ ΠΏΠ΅ΡΠ΅Π·Π°ΠΏΠΈΡΠ°Π½ΠΎ Π·Π½Π°ΡΠ΅Π½Π½ΡΠΌ "b"
1.5 => 'c', // Π·Π½Π°ΡΠ΅Π½Π½Ρ "b" Π±ΡΠ΄Π΅ ΠΏΠ΅ΡΠ΅Π·Π°ΠΏΠΈΡΠ°Π½ΠΎ Π·Π½Π°ΡΠ΅Π½Π½ΡΠΌ "c"
-1 => 'd',
'01' => 'e', // ΠΎΡΠΊΡΠ»ΡΠΊΠΈ ΠΊΠ»ΡΡ Π½Π΅ Ρ ΡΡΠ»ΠΈΠΌ ΡΠΈΡΠ»ΠΎΠΌ, Π²ΡΠ½ ΠΠ ΠΏΠ΅ΡΠ΅Π·Π°ΠΏΠΈΡΠ΅ ΠΊΠ»ΡΡ Π΄Π»Ρ 1
'1.5' => 'f', // ΠΎΡΠΊΡΠ»ΡΠΊΠΈ ΠΊΠ»ΡΡ Π½Π΅ Ρ ΡΡΠ»ΠΈΠΌ ΡΠΈΡΠ»ΠΎΠΌ, Π²ΡΠ½ ΠΠ ΠΏΠ΅ΡΠ΅Π·Π°ΠΏΠΈΡΠ΅ ΠΊΠ»ΡΡ Π΄Π»Ρ 1
true => 'g', // Π·Π½Π°ΡΠ΅Π½Π½Ρ "c" Π±ΡΠ΄Π΅ ΠΏΠ΅ΡΠ΅Π·Π°ΠΏΠΈΡΠ°Π½ΠΎ Π·Π½Π°ΡΠ΅Π½Π½ΡΠΌ "g"
false => 'h',
'' => 'i',
null => 'j', // Π·Π½Π°ΡΠ΅Π½Π½Ρ "i" Π±ΡΠ΄Π΅ ΠΏΠ΅ΡΠ΅Π·Π°ΠΏΠΈΡΠ°Π½ΠΎ Π·Π½Π°ΡΠ΅Π½Π½ΡΠΌ "j"
'k', // Π·Π½Π°ΡΠ΅Π½Π½Ρ "k" Π±ΡΠ΄Π΅ ΠΏΡΠΈΡΠ²ΠΎΡΠ½ΠΎ ΠΊΠ»ΡΡ 2. Π¦Π΅ ΡΠΎΠΌΡ, ΡΠΎ Π½Π°ΠΉΠ±ΡΠ»ΡΡΠΈΠΉ ΠΊΠ»ΡΡ ΡΡΠ»ΠΎΠ³ΠΎ ΡΠΈΡΠ»Π° Π΄ΠΎ ΡΡΠΎΠ³ΠΎ Π±ΡΠ² 1
2 => 'l', // Π·Π½Π°ΡΠ΅Π½Π½Ρ "k" Π±ΡΠ΄Π΅ ΠΏΠ΅ΡΠ΅Π·Π°ΠΏΠΈΡΠ°Π½ΠΎ Π·Π½Π°ΡΠ΅Π½Π½ΡΠΌ "l"
);
var_dump($array);
?>ΠΠΎΠ΄Π°Π½ΠΈΠΉ Π²ΠΈΡΠ΅ ΠΏΡΠΈΠΊΠ»Π°Π΄ Π²ΠΈΠ²Π΅Π΄Π΅:
array(7) {
[1]=>
string(1) "g"
[-1]=>
string(1) "d"
["01"]=>
string(1) "e"
["1.5"]=>
string(1) "f"
[0]=>
string(1) "h"
[""]=>
string(1) "j"
[2]=>
string(1) "l"
}
ΠΠΎΡΡΡΠΏ Π΄ΠΎ Π΅Π»Π΅ΠΌΠ΅Π½ΡΡΠ² ΠΌΠ°ΡΠΈΠ²Ρ ΠΌΠΎΠΆΠ½Π° ΠΎΡΡΠΈΠΌΠ°ΡΠΈ Π·Π° Π΄ΠΎΠΏΠΎΠΌΠΎΠ³ΠΎΡ ΡΠΈΠ½ΡΠ°ΠΊΡΠΈΡΡ array[key].
ΠΡΠΈΠΊΠ»Π°Π΄ #7 ΠΠΎΡΡΡΠΏ Π΄ΠΎ Π΅Π»Π΅ΠΌΠ΅Π½ΡΡΠ² ΠΌΠ°ΡΠΈΠ²Ρ
<?php
$array = array(
"foo" => "bar",
42 => 24,
"multi" => array(
"dimensional" => array(
"array" => "foo"
)
)
);
var_dump($array["foo"]);
var_dump($array[42]);
var_dump($array["multi"]["dimensional"]["array"]);
?>ΠΠΎΠ΄Π°Π½ΠΈΠΉ Π²ΠΈΡΠ΅ ΠΏΡΠΈΠΊΠ»Π°Π΄ Π²ΠΈΠ²Π΅Π΄Π΅:
string(3) "bar" int(24) string(3) "foo"
ΠΠ°ΡΠ²Π°ΠΆΠ΅Π½Π½Ρ:
ΠΠΎ PHP 8.0.0 ΠΊΠ²Π°Π΄ΡΠ°ΡΠ½Ρ ΡΠ° ΡΡΠ³ΡΡΠ½Ρ Π΄ΡΠΆΠΊΠΈ ΠΌΠΎΠ³Π»ΠΈ Π²ΠΈΠΊΠΎΡΠΈΡΡΠΎΠ²ΡΠ²Π°ΡΠΈΡΡ ΡΠΊ Π²Π·Π°ΡΠΌΠΎΠ·Π°ΠΌΡΠ½Π½Ρ Π΄Π»Ρ Π΄ΠΎΡΡΡΠΏΡ Π΄ΠΎ Π΅Π»Π΅ΠΌΠ΅Π½ΡΡΠ² ΠΌΠ°ΡΠΈΠ²Ρ (Π½Π°ΠΏΡΠΈΠΊΠ»Π°Π΄,
$array[42]Ρ$array{42}ΡΠΎΠ±ΠΈΠ»ΠΈ Π± ΡΠ΅ ΡΠ°ΠΌΠ΅ Ρ ΠΏΡΠΈΠΊΠ»Π°Π΄Ρ Π²ΠΈΡΠ΅). Π‘ΠΈΠ½ΡΠ°ΠΊΡΠΈΡ ΡΡΠ³ΡΡΠ½ΠΈΡ Π΄ΡΠΆΠΎΠΊ Π·Π°ΡΡΠ°ΡΡΠ² ΡΠ· PHP 7.4.0 Ρ Π±ΡΠ»ΡΡΠ΅ Π½Π΅ ΠΏΡΠ΄ΡΡΠΈΠΌΡΡΡΡΡΡ Π· PHP 8.0.0.
ΠΡΠΈΠΊΠ»Π°Π΄ #8 Π ΠΎΠ·ΡΠΌΠ΅Π½ΡΠ²Π°Π½Π½Ρ ΠΌΠ°ΡΠΈΠ²Ρ
<?php
function getArray() {
return array(1, 2, 3);
}
$secondElement = getArray()[1];
?>ΠΠ°ΡΠ²Π°ΠΆΠ΅Π½Π½Ρ:
Π‘ΠΏΡΠΎΠ±Π° ΠΎΡΡΠΈΠΌΠ°ΡΠΈ Π΄ΠΎΡΡΡΠΏ Π΄ΠΎ ΠΊΠ»ΡΡΠ° ΠΌΠ°ΡΠΈΠ²Ρ, ΡΠΊΠΈΠΉ Π½Π΅ Π±ΡΠ»ΠΎ Π²ΠΈΠ·Π½Π°ΡΠ΅Π½ΠΎ, ΡΠ΅ ΡΠ΅ΠΆ ΡΠ°ΠΌΠ΅, ΡΠΊ Π΄ΠΎΡΡΡΠΏ Π΄ΠΎ Π±ΡΠ΄Ρ-ΡΠΊΠΎΡ ΡΠ½ΡΠΎΡ Π½Π΅Π²ΠΈΠ·Π½Π°ΡΠ΅Π½ΠΎΡ Π·ΠΌΡΠ½Π½ΠΎΡ: Π±ΡΠ΄Π΅ Π²ΠΈΠ΄Π°Π½ΠΎ ΠΏΠΎΠΌΠΈΠ»ΠΊΡ ΡΡΠ²Π½Ρ
E_WARNING(ΡΡΠ²Π΅Π½ΡE_NOTICEΠ΄ΠΎ PHP 8.0.0), Ρ ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ Π±ΡΠ΄Π΅null.
ΠΠ°ΡΠ²Π°ΠΆΠ΅Π½Π½Ρ:
Π ΠΎΠ·ΡΠΌΠ΅Π½ΡΠ²Π°Π½Π½Ρ ΠΌΠ°ΡΠΈΠ²Ρ ΡΠΊΠ°Π»ΡΡΠ½ΠΎΠ³ΠΎ Π·Π½Π°ΡΠ΅Π½Π½Ρ, ΡΠΊΠ΅ Π½Π΅ Ρ ΡΡΠ΄ΠΊΠΎΠΌ (string), Π΄Π°Ρ
null. ΠΠΎ PHP 7.4.0 ΡΠ΅ Π½Π΅ Π²ΠΈΠ΄Π°Π²Π°Π»ΠΎ ΠΏΠΎΠ²ΡΠ΄ΠΎΠΌΠ»Π΅Π½Π½Ρ ΠΏΡΠΎ ΠΏΠΎΠΌΠΈΠ»ΠΊΡ. ΠΠΎΡΠΈΠ½Π°ΡΡΠΈ Π· PHP 7.4.0, ΡΠ΅ Π²ΠΈΠ΄Π°Ρ ΠΏΠΎΠΌΠΈΠ»ΠΊΡ ΡΡΠ²Π½ΡE_NOTICE; ΠΏΠΎΡΠΈΠ½Π°ΡΡΠΈ Π· PHP 8.0.0, ΡΠ΅ Π²ΠΈΠ΄Π°Ρ ΠΏΠΎΠΌΠΈΠ»ΠΊΡ ΡΡΠ²Π½ΡE_WARNING.
ΠΡΠ½ΡΡΡΠΈΠΉ ΠΌΠ°ΡΠΈΠ² ΠΌΠΎΠΆΠ½Π° Π·ΠΌΡΠ½ΠΈΡΠΈ, ΡΠ²Π½ΠΎ Π·Π°Π΄Π°Π²ΡΠΈ Π² Π½ΡΠΎΠΌΡ Π·Π½Π°ΡΠ΅Π½Π½Ρ.
Π¦Π΅ ΡΠΎΠ±ΠΈΡΡΡΡ ΡΠ»ΡΡ
ΠΎΠΌ ΠΏΡΠΈΡΠ²ΠΎΡΠ½Π½Ρ Π·Π½Π°ΡΠ΅Π½Ρ ΠΌΠ°ΡΠΈΠ²Ρ (array) ΡΠ· Π·Π°Π·Π½Π°ΡΠ΅Π½Π½ΡΠΌ
ΠΊΠ»ΡΡΠ° Π² Π΄ΡΠΆΠΊΠ°Ρ
. ΠΠ»ΡΡ ΡΠ°ΠΊΠΎΠΆ ΠΌΠΎΠΆΠ½Π° ΠΎΠΏΡΡΡΠΈΡΠΈ, ΡΠΎ ΠΏΡΠΈΠ·Π²Π΅Π΄Π΅ Π΄ΠΎ ΠΏΡΡΡΠΎΡ ΠΏΠ°ΡΠΈ Π΄ΡΠΆΠΎΠΊ ([]).
$arr[key] = value; $arr[] = value; // ΠΠ»ΡΡ key ΠΌΠΎΠΆΠ΅ Π±ΡΡΠΈ ΡΠΈΠΏΠΎΠΌ int Π°Π±ΠΎ string // ΠΠ½Π°ΡΠ΅Π½Π½Ρ value ΠΌΠΎΠΆΠ΅ Π±ΡΡΠΈ Π±ΡΠ΄Ρ-ΡΠΊΠΈΠΌ Π·Π½Π°ΡΠ΅Π½Π½ΡΠΌ Π±ΡΠ΄Ρ-ΡΠΊΠΎΠ³ΠΎ ΡΠΈΠΏΡ
Π―ΠΊΡΠΎ $arr ΡΠ΅ Π½Π΅ ΡΡΠ½ΡΡ Π°Π±ΠΎ ΠΌΠ°Ρ Π·Π½Π°ΡΠ΅Π½Π½Ρ null Π°Π±ΠΎ false, Π²ΡΠ½ Π±ΡΠ΄Π΅ ΡΡΠ²ΠΎΡΠ΅Π½ΠΈΠΉ,
ΡΠΎΠΌΡ ΡΠ΅ ΡΠ°ΠΊΠΎΠΆ Π°Π»ΡΡΠ΅ΡΠ½Π°ΡΠΈΠ²Π½ΠΈΠΉ ΡΠΏΠΎΡΡΠ± ΡΡΠ²ΠΎΡΠ΅Π½Π½Ρ ΠΌΠ°ΡΠΈΠ²Ρ (array). ΠΠ΄Π½Π°ΠΊ ΡΠ°ΠΊΠ° ΠΏΡΠ°ΠΊΡΠΈΠΊΠ°
Π½Π΅ ΡΠ΅ΠΊΠΎΠΌΠ΅Π½Π΄ΡΡΡΡΡΡ, ΠΎΡΠΊΡΠ»ΡΠΊΠΈ ΡΠΊΡΠΎ $arr Π²ΠΆΠ΅ ΠΌΡΡΡΠΈΡΡ ΠΏΠ΅Π²Π½Π΅ Π·Π½Π°ΡΠ΅Π½Π½Ρ (Π½Π°ΠΏΡΠΈΠΊΠ»Π°Π΄,
ΡΡΠ΄ΠΎΠΊ (string) ΡΠ· Π·ΠΌΡΠ½Π½ΠΎΡ Π·Π°ΠΏΠΈΡΡ), ΡΠΎ ΡΠ΅ Π·Π½Π°ΡΠ΅Π½Π½Ρ Π·Π°Π»ΠΈΡΠΈΡΡΡΡ Π½Π° ΠΌΡΡΡΡ, Π° []
ΠΌΠΎΠΆΠ΅ ΡΠ°ΠΊΡΠΈΡΠ½ΠΎ ΠΎΠ·Π½Π°ΡΠ°ΡΠΈ ΠΎΠΏΠ΅ΡΠ°ΡΠΎΡ Π΄ΠΎΡΡΡΠΏΡ Π΄ΠΎ ΡΡΠ΄ΠΊΠ°.
ΠΠ°Π²ΠΆΠ΄ΠΈ ΠΊΡΠ°ΡΠ΅ ΡΠ½ΡΡΡΠ°Π»ΡΠ·ΡΠ²Π°ΡΠΈ Π·ΠΌΡΠ½Π½Ρ ΠΏΡΡΠΌΠΈΠΌ ΠΏΡΠΈΡΠ²ΠΎΡΠ½Π½ΡΠΌ.
ΠΠ°ΡΠ²Π°ΠΆΠ΅Π½Π½Ρ: ΠΠΎΡΠΈΠ½Π°ΡΡΠΈ Π· PHP 7.1.0, Π·Π°ΡΡΠΎΡΡΠ²Π°Π½Π½Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΎΡΠ° ΠΏΠΎΡΠΎΠΆΠ½ΡΠΎΠ³ΠΎ ΡΠ½Π΄Π΅ΠΊΡΡ Π΄ΠΎ ΡΡΠ΄ΠΊΠ° Π²ΠΈΠΊΠ»ΠΈΠΊΠ°Ρ ΡΠ°ΡΠ°Π»ΡΠ½Ρ ΠΏΠΎΠΌΠΈΠ»ΠΊΡ. Π Π°Π½ΡΡΠ΅ ΡΡΠ΄ΠΎΠΊ Π°Π²ΡΠΎΠΌΠ°ΡΠΈΡΠ½ΠΎ ΠΏΠ΅ΡΠ΅ΡΠ²ΠΎΡΡΠ²Π°Π²ΡΡ Π½Π° ΠΌΠ°ΡΠΈΠ².
ΠΠ°ΡΠ²Π°ΠΆΠ΅Π½Π½Ρ: ΠΠΎΡΠΈΠ½Π°ΡΡΠΈ Π· PHP 8.1.0, ΡΡΠ²ΠΎΡΠ΅Π½Π½Ρ Π½ΠΎΠ²ΠΎΠ³ΠΎ ΠΌΠ°ΡΠΈΠ²Ρ ΠΏΡΠΈΠ²Π΅Π΄Π΅Π½Π½ΡΠΌ Π΄ΠΎ Π½ΡΠΎΠ³ΠΎ Π·Π½Π°ΡΠ΅Π½Π½Ρ
falseΠ·Π°ΡΡΠ°ΡΡΠ»ΠΎ. Π‘ΡΠ²ΠΎΡΠ΅Π½Π½Ρ Π½ΠΎΠ²ΠΎΠ³ΠΎ ΠΌΠ°ΡΠΈΠ²Ρ ΡΠ·nullΡ Π½Π΅Π²ΠΈΠ·Π½Π°ΡΠ΅Π½ΠΈΡ Π·Π½Π°ΡΠ΅Π½Ρ Π²ΡΠ΅ ΡΠ΅ Π΄ΠΎΠ·Π²ΠΎΠ»Π΅Π½ΠΎ.
Π©ΠΎΠ± Π·ΠΌΡΠ½ΠΈΡΠΈ ΠΏΠ΅Π²Π½Π΅ Π·Π½Π°ΡΠ΅Π½Π½Ρ, ΠΏΡΠΈΠ·Π½Π°ΡΡΠ΅ Π½ΠΎΠ²Π΅ Π·Π½Π°ΡΠ΅Π½Π½Ρ ΡΡΠΎΠΌΡ Π΅Π»Π΅ΠΌΠ΅Π½ΡΡ Π·Π° Π΄ΠΎΠΏΠΎΠΌΠΎΠ³ΠΎΡ ΠΉΠΎΠ³ΠΎ ΠΊΠ»ΡΡΠ°. Π©ΠΎΠ± Π²ΠΈΠ΄Π°Π»ΠΈΡΠΈ ΠΏΠ°ΡΡ ΠΊΠ»ΡΡ/Π·Π½Π°ΡΠ΅Π½Π½Ρ, Π²ΠΈΠΊΠ»ΠΈΠΊΠ°ΠΉΡΠ΅ Π΄Π»Ρ Π½Π΅Ρ ΡΡΠ½ΠΊΡΡΡ unset().
<?php
$arr = array(5 => 1, 12 => 2);
$arr[] = 56; // Π¦Π΅ ΡΠ΅ ΡΠ°ΠΌΠ΅, ΡΠΎ $arr[13] = 56;
// Π½Π° ΡΡΠΎΠΌΡ Π΅ΡΠ°ΠΏΡ ΡΠΊΡΠΈΠΏΡΠ°
$arr["x"] = 42; // Π¦Π΅ Π΄ΠΎΠ΄Π°Ρ Π½ΠΎΠ²ΠΈΠΉ Π΅Π»Π΅ΠΌΠ΅Π½Ρ
// Π΄ΠΎ ΠΌΠ°ΡΠΈΠ²Ρ ΡΠ· ΠΊΠ»ΡΡΠ΅ΠΌ "x"
unset($arr[5]); // Π¦Π΅ Π²ΠΈΠ΄Π°Π»ΡΡ Π΅Π»Π΅ΠΌΠ΅Π½Ρ ΡΠ· ΠΌΠ°ΡΠΈΠ²Ρ
unset($arr); // Π¦Π΅ Π²ΠΈΠ΄Π°Π»ΡΡ Π²Π΅ΡΡ ΠΌΠ°ΡΠΈΠ²
?>ΠΠ°ΡΠ²Π°ΠΆΠ΅Π½Π½Ρ:
Π―ΠΊ Π·Π³Π°Π΄ΡΠ²Π°Π»ΠΎΡΡ Π²ΠΈΡΠ΅, ΡΠΊΡΠΎ ΠΊΠ»ΡΡ Π½Π΅ Π²ΠΊΠ°Π·Π°Π½ΠΎ, Π±Π΅ΡΠ΅ΡΡΡΡ ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡΠ½ΠΈΠΉ Π· ΡΡΠ½ΡΡΡΠΈΡ ΡΡΠ»ΠΎΡΠΈΡΠ΅Π»ΡΠ½ΠΈΡ ΡΠ½Π΄Π΅ΠΊΡΡΠ² int, Ρ Π½ΠΎΠ²ΠΈΠΌ ΠΊΠ»ΡΡΠ΅ΠΌ Π±ΡΠ΄Π΅ ΡΠ΅ ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡΠ½Π΅ Π·Π½Π°ΡΠ΅Π½Π½Ρ ΠΏΠ»ΡΡ 1 (Π°Π»Π΅ ΠΏΡΠΈΠ½Π°ΠΉΠΌΠ½Ρ 0). Π―ΠΊΡΠΎ ΡΡΠ»ΠΎΡΠΈΡΠ΅Π»ΡΠ½ΠΈΡ ΡΠ½Π΄Π΅ΠΊΡΡΠ² int ΡΠ΅ Π½Π΅ ΡΡΠ½ΡΡ, ΠΊΠ»ΡΡ Π±ΡΠ΄Π΅
0(Π½ΡΠ»Ρ).ΠΠ²Π΅ΡΠ½ΡΡΡ ΡΠ²Π°Π³Ρ, ΡΠΎ ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡΠ½ΠΈΠΉ ΡΡΠ»ΠΎΡΠΈΡΠ΅Π»ΡΠ½ΠΈΠΉ ΠΊΠ»ΡΡ, ΡΠΊΠΈΠΉ Π²ΠΈΠΊΠΎΡΠΈΡΡΠΎΠ²ΡΡΡΡΡΡ Π΄Π»Ρ ΡΡΠΎΠ³ΠΎ, Π½Π°ΡΠ°Π·Ρ Π½Π΅ ΠΎΠ±ΠΎΠ²βΡΠ·ΠΊΠΎΠ²ΠΎ ΡΡΠ½ΡΡ Π² ΠΌΠ°ΡΠΈΠ²Ρ.. ΠΡΠ½ ΠΏΠΎΠ²ΠΈΠ½Π΅Π½ ΡΡΠ½ΡΠ²Π°ΡΠΈ Π² ΠΌΠ°ΡΠΈΠ²Ρ Π»ΠΈΡΠ΅ Π΄Π΅ΡΠΊΠΈΠΉ ΡΠ°Ρ Π· ΠΌΠΎΠΌΠ΅Π½ΡΡ ΠΎΡΡΠ°Π½Π½ΡΠΎΠ³ΠΎ ΠΏΠΎΠ²ΡΠΎΡΠ½ΠΎΠ³ΠΎ ΡΠ½Π΄Π΅ΠΊΡΡΠ²Π°Π½Π½Ρ ΠΌΠ°ΡΠΈΠ²Ρ. ΠΠ°ΡΡΡΠΏΠ½ΠΈΠΉ ΠΏΡΠΈΠΊΠ»Π°Π΄ ΡΠ»ΡΡΡΡΡΡ:
<?php
// Π‘ΡΠ²ΠΎΡΡΡΠΌΠΎ ΠΏΡΠΎΡΡΠΈΠΉ ΠΌΠ°ΡΠΈΠ².
$array = array(1, 2, 3, 4, 5);
print_r($array);
// Π’Π΅ΠΏΠ΅Ρ Π²ΠΈΠ΄Π°Π»ΠΈΠΌΠΎ ΡΡΡ Π΅Π»Π΅ΠΌΠ΅Π½ΡΠΈ, Π°Π»Π΅ Π·Π°Π»ΠΈΡΠΈΠΌΠΎ ΡΠ°ΠΌ ΠΌΠ°ΡΠΈΠ² Π±Π΅Π· Π·ΠΌΡΠ½:
foreach ($array as $i => $value) {
unset($array[$i]);
}
print_r($array);
// ΠΠΎΠ΄Π°ΡΠΌΠΎ Π΅Π»Π΅ΠΌΠ΅Π½Ρ (Π·Π²Π΅ΡΠ½ΡΡΡ ΡΠ²Π°Π³Ρ, ΡΠΎ Π½ΠΎΠ²ΠΈΠΉ ΠΊΠ»ΡΡ ΠΌΠ°Ρ Π·Π½Π°ΡΠ΅Π½Π½Ρ 5, Π° Π½Π΅ 0).
$array[] = 6;
print_r($array);
// ΠΠ΅ΡΠ΅ΡΠ½Π΄Π΅ΠΊΡΠ°ΡΡΡ:
$array = array_values($array);
$array[] = 7;
print_r($array);
?>ΠΠΎΠ΄Π°Π½ΠΈΠΉ Π²ΠΈΡΠ΅ ΠΏΡΠΈΠΊΠ»Π°Π΄ Π²ΠΈΠ²Π΅Π΄Π΅:
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 ) Array ( ) Array ( [5] => 6 ) Array ( [0] => 6 [1] => 7 )
ΠΠ°ΡΠΈΠ²ΠΈ ΠΌΠΎΠΆΠ½Π° Π΄Π΅ΡΡΡΡΠΊΡΡΡΡΠ²Π°ΡΠΈ Π·Π° Π΄ΠΎΠΏΠΎΠΌΠΎΠ³ΠΎΡ ΠΌΠΎΠ²Π½ΠΈΡ
ΠΊΠΎΠ½ΡΡΡΡΠΊΡΡΠΉ []
(ΠΏΠΎΡΠΈΠ½Π°ΡΡΠΈ Π· PHP 7.1.0) Π°Π±ΠΎ list(). Π¦Ρ ΠΊΠΎΠ½ΡΡΡΡΠΊΡΡΡ ΠΌΠΎΠΆΠ½Π°
Π²ΠΈΠΊΠΎΡΠΈΡΡΠΎΠ²ΡΠ²Π°ΡΠΈ Π΄Π»Ρ Π΄Π΅ΡΡΡΡΠΊΡΡΡΡΠ²Π°Π½Π½Ρ ΠΌΠ°ΡΠΈΠ²Ρ Π½Π° ΠΎΠΊΡΠ΅ΠΌΡ Π·ΠΌΡΠ½Π½Ρ.
<?php
$source_array = ['foo', 'bar', 'baz'];
[$foo, $bar, $baz] = $source_array;
echo $foo; // Π²ΠΈΠ²Π΅Π΄Π΅ "foo"
echo $bar; // Π²ΠΈΠ²Π΅Π΄Π΅ "bar"
echo $baz; // Π²ΠΈΠ²Π΅Π΄Π΅ "baz"
?>ΠΠ΅ΡΡΡΡΠΊΡΡΡΠΈΠ·Π°ΡΡΡ ΠΌΠ°ΡΠΈΠ²Ρ ΠΌΠΎΠΆΠ½Π° Π²ΠΈΠΊΠΎΡΠΈΡΡΠΎΠ²ΡΠ²Π°ΡΠΈ Ρ foreach Π΄Π»Ρ Π΄Π΅ΡΡΡΡΠΊΡΡΡΠΈΠ·Π°ΡΡΡ Π±Π°Π³Π°ΡΠΎΠ²ΠΈΠΌΡΡΠ½ΠΎΠ³ΠΎ ΠΌΠ°ΡΠΈΠ²Ρ ΠΏΡΠ΄ ΡΠ°Ρ ΡΡΠ΅ΡΠ°ΡΡΡ ΠΏΠΎ Π½ΡΠΎΠΌΡ.
<?php
$source_array = [
[1, 'John'],
[2, 'Jane'],
];
foreach ($source_array as [$id, $name]) {
// Π»ΠΎΠ³ΡΠΊΠ° ΡΠΎΠ±ΠΎΡΠΈ ΡΠ· $id Ρ $name
}
?>
ΠΠ»Π΅ΠΌΠ΅Π½ΡΠΈ ΠΌΠ°ΡΠΈΠ²Ρ ΡΠ³Π½ΠΎΡΡΠ²Π°ΡΠΈΠΌΡΡΡΡΡ, ΡΠΊΡΠΎ Π·ΠΌΡΠ½Π½Π° Π½Π΅ Π²ΠΊΠ°Π·Π°Π½Π°.
ΠΠ΅ΡΡΡΡΠΊΡΡΡΠΈΠ·Π°ΡΡΡ ΠΌΠ°ΡΠΈΠ²Ρ Π·Π°Π²ΠΆΠ΄ΠΈ ΠΏΠΎΡΠΈΠ½Π°ΡΡΡΡΡ Π· ΡΠ½Π΄Π΅ΠΊΡΡ 0.
<?php
$source_array = ['foo', 'bar', 'baz'];
// ΠΡΠΈΠ·Π½Π°ΡΠΌΠΎ Π΅Π»Π΅ΠΌΠ΅Π½Ρ Π· ΡΠ½Π΄Π΅ΠΊΡΠΎΠΌ 2 Π΄ΠΎ Π·ΠΌΡΠ½Π½ΠΎΡ $baz
[, , $baz] = $source_array;
echo $baz; // Π²ΠΈΠ²Π΅Π΄Π΅ "baz"
?>ΠΠΎΡΠΈΠ½Π°ΡΡΠΈ Π· PHP 7.1.0, Π°ΡΠΎΡΡΠ°ΡΠΈΠ²Π½Ρ ΠΌΠ°ΡΠΈΠ²ΠΈ ΡΠ°ΠΊΠΎΠΆ ΠΌΠΎΠΆΠ½Π° Π΄Π΅ΡΡΡΡΠΊΡΡΡΡΠ²Π°ΡΠΈ. Π¦Π΅ ΡΠ°ΠΊΠΎΠΆ Π΄ΠΎΠ·Π²ΠΎΠ»ΡΡ Π»Π΅Π³ΡΠ΅ Π²ΠΈΠ±ΠΈΡΠ°ΡΠΈ ΠΏΠΎΡΡΡΠ±Π½ΠΈΠΉ Π΅Π»Π΅ΠΌΠ΅Π½Ρ Ρ ΠΌΠ°ΡΠΈΠ²Π°Ρ ΡΠ· ΡΠΈΡΠ»ΠΎΠ²ΠΈΠΌΠΈ ΡΠ½Π΄Π΅ΠΊΡΠ°ΠΌΠΈ, ΠΎΡΠΊΡΠ»ΡΠΊΠΈ ΡΠ½Π΄Π΅ΠΊΡ ΠΌΠΎΠΆΠ½Π° Π²ΠΊΠ°Π·Π°ΡΠΈ ΡΠ²Π½ΠΎ.
<?php
$source_array = ['foo' => 1, 'bar' => 2, 'baz' => 3];
// ΠΡΠΈΠ·Π½Π°ΡΠ°ΡΠΌΠΎ Π΅Π»Π΅ΠΌΠ΅Π½Ρ Π· ΡΠ½Π΄Π΅ΠΊΡΠΎΠΌ 'baz' Π΄ΠΎ Π·ΠΌΡΠ½Π½ΠΎΡ $three
['baz' => $three] = $source_array;
echo $three; // Π²ΠΈΠ²Π΅Π΄Π΅ 3
$source_array = ['foo', 'bar', 'baz'];
// ΠΡΠΈΠ·Π½Π°ΡΠ°ΡΠΌΠΎ Π΅Π»Π΅ΠΌΠ΅Π½Ρ Π· ΡΠ½Π΄Π΅ΠΊΡΠΎΠΌ 2 Π΄ΠΎ Π·ΠΌΡΠ½Π½ΠΎΡ $baz
[2 => $baz] = $source_array;
echo $baz; // Π²ΠΈΠ²Π΅Π΄Π΅ "baz"
?>ΠΠ΅ΡΡΡΡΠΊΡΡΡΠΈΠ·Π°ΡΡΡ ΠΌΠ°ΡΠΈΠ²Ρ ΠΌΠΎΠΆΠ΅ Π±ΡΡΠΈ Π²ΠΈΠΊΠΎΡΠΈΡΡΠ°Π½Π° Π΄Π»Ρ Π»Π΅Π³ΠΊΠΎΡ Π·Π°ΠΌΡΠ½ΠΈ Π΄Π²ΠΎΡ Π·ΠΌΡΠ½Π½ΠΈΡ ..
<?php
$a = 1;
$b = 2;
[$b, $a] = [$a, $b];
echo $a; // Π²ΠΈΠ²Π΅Π΄Π΅ 2
echo $b; // Π²ΠΈΠ²Π΅Π΄Π΅ 1
?>ΠΠ°ΡΠ²Π°ΠΆΠ΅Π½Π½Ρ:
ΠΠΏΠ΅ΡΠ°ΡΠΎΡ (
...) Π½Π΅ ΠΏΡΠ΄ΡΡΠΈΠΌΡΡΡΡΡΡ Ρ ΠΏΡΠΈΠ·Π½Π°ΡΠ΅Π½Π½ΡΡ .
ΠΠ°ΡΠ²Π°ΠΆΠ΅Π½Π½Ρ:
Π‘ΠΏΡΠΎΠ±Π° ΠΎΡΡΠΈΠΌΠ°ΡΠΈ Π΄ΠΎΡΡΡΠΏ Π΄ΠΎ ΠΊΠ»ΡΡΠ° ΠΌΠ°ΡΠΈΠ²Ρ, ΡΠΊΠΈΠΉ Π½Π΅ Π±ΡΠ»ΠΎ Π²ΠΈΠ·Π½Π°ΡΠ΅Π½ΠΎ, Ρ ΡΠ°ΠΊΠΎΡ ΡΠ°ΠΌΠΎΡ, ΡΠΊ Π΄ΠΎΡΡΡΠΏ Π΄ΠΎ Π±ΡΠ΄Ρ-ΡΠΊΠΎΡ ΡΠ½ΡΠΎΡ Π½Π΅Π²ΠΈΠ·Π½Π°ΡΠ΅Π½ΠΎΡ Π·ΠΌΡΠ½Π½ΠΎΡ: Π±ΡΠ΄Π΅ Π²ΠΈΠ΄Π°Π½ΠΎ ΠΏΠΎΠ²ΡΠ΄ΠΎΠΌΠ»Π΅Π½Π½Ρ ΠΏΡΠΎ ΠΏΠΎΠΌΠΈΠ»ΠΊΡ ΡΡΠ²Π½Ρ
E_WARNING(ΡΡΠ²Π΅Π½ΡE_NOTICEΠ΄ΠΎ PHP 8.0.0), Ρ ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ Π±ΡΠ΄Π΅null.
ΠΡΠ½ΡΡ Π΄ΠΎΡΠΈΡΡ Π±Π°Π³Π°ΡΠΎ ΠΊΠΎΡΠΈΡΠ½ΠΈΡ ΡΡΠ½ΠΊΡΡΠΉ Π΄Π»Ρ ΡΠΎΠ±ΠΎΡΠΈ Π· ΠΌΠ°ΡΠΈΠ²Π°ΠΌΠΈ. ΠΠΈΠ²ΡΡΡΡΡ ΡΠΎΠ·Π΄ΡΠ» "Π€ΡΠ½ΠΊΡΡΡ Π΄Π»Ρ ΡΠΎΠ±ΠΎΡΠΈ Π· ΠΌΠ°ΡΠΈΠ²Π°ΠΌΠΈ".
ΠΠ°ΡΠ²Π°ΠΆΠ΅Π½Π½Ρ:
Π€ΡΠ½ΠΊΡΡΡ unset() Π΄ΠΎΠ·Π²ΠΎΠ»ΡΡ Π²ΠΈΠ΄Π°Π»ΡΡΠΈ ΠΊΠ»ΡΡΡ Π· ΠΌΠ°ΡΠΈΠ²Ρ. ΠΠ°ΠΉΡΠ΅ Π½Π° ΡΠ²Π°Π·Ρ, ΡΠΎ ΠΌΠ°ΡΠΈΠ² ΠΠ Π±ΡΠ΄Π΅ ΠΏΠΎΠ²ΡΠΎΡΠ½ΠΎ ΡΠ½Π΄Π΅ΠΊΡΠΎΠ²Π°Π½ΠΈΠΉ. Π―ΠΊΡΠΎ ΠΏΠΎΡΡΡΠ±Π½Π° ΡΠΏΡΠ°Π²ΠΆΠ½Ρ ΠΏΠΎΠ²Π΅Π΄ΡΠ½ΠΊΠ° Β«Π²ΠΈΠ΄Π°Π»Π΅Π½Π½Ρ ΡΠ° Π·ΠΌΡΡΠ΅Π½Π½ΡΒ», ΠΌΠ°ΡΠΈΠ² ΠΌΠΎΠΆΠ½Π° ΠΏΠ΅ΡΠ΅ΡΠ½Π΄Π΅ΠΊΡΡΠ²Π°ΡΠΈ Π·Π° Π΄ΠΎΠΏΠΎΠΌΠΎΠ³ΠΎΡ ΡΡΠ½ΠΊΡΡΡ array_values().
<?php
$a = array(1 => 'one', 2 => 'two', 3 => 'three');
unset($a[2]);
/* ΡΡΠ²ΠΎΡΠΈΡΡ ΠΌΠ°ΡΠΈΠ², ΡΠΊΠΈΠΉ Π±ΡΠ² Π±ΠΈ Π²ΠΈΠ·Π½Π°ΡΠ΅Π½ΠΈΠΉ ΡΠΊ:
$a = array(1 => 'one', 3 => 'three');
a ΠΠ ΡΠ°ΠΊ:
$a = array(1 => 'one', 2 => 'three');
*/
$b = array_values($a);
// Π’Π΅ΠΏΠ΅Ρ $b ΡΠ΅ array(0 => 'one', 1 => 'three')
?>
ΠΠ΅ΡΡΡΡΠ° ΡΡΡΡΠΊΡΡΡΠ° foreach ΡΡΠ½ΡΡ ΡΠΏΠ΅ΡΡΠ°Π»ΡΠ½ΠΎ Π΄Π»Ρ ΠΌΠ°ΡΠΈΠ²ΡΠ². ΠΠΎΠ½Π° Π·Π°Π±Π΅Π·ΠΏΠ΅ΡΡΡ ΠΏΡΠΎΡΡΠΈΠΉ ΡΠΏΠΎΡΡΠ± ΠΎΠ±Ρ ΠΎΠ΄Ρ ΠΌΠ°ΡΠΈΠ²Ρ.
$foo[bar] wrong?
Always use quotes around a string literal array index. For example,
$foo['bar'] is correct, while
$foo[bar] is not. But why? It is common to encounter this
kind of syntax in old scripts:
<?php
$foo[bar] = 'enemy';
echo $foo[bar];
// etc
?>
This is wrong, but it works. The reason is that this code has an undefined
constant (bar) rather than a string ('bar' - notice the
quotes). It works because PHP automatically converts a
bare string (an unquoted string which does
not correspond to any known symbol) into a string which
contains the bare string. For instance, if there is no defined
constant named bar, then PHP will substitute in the
string 'bar' and use that.
The fallback to treat an undefined constant as bare string issues an error
of level E_NOTICE.
This has been deprecated as of PHP 7.2.0, and issues an error
of level E_WARNING.
As of PHP 8.0.0, it has been removed and throws an
Error exception.
ΠΠ°ΡΠ²Π°ΠΆΠ΅Π½Π½Ρ: This does not mean to always quote the key. Do not quote keys which are constants or variables, as this will prevent PHP from interpreting them.
<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
ini_set('html_errors', false);
// Simple array:
$array = array(1, 2);
$count = count($array);
for ($i = 0; $i < $count; $i++) {
echo "\nChecking $i: \n";
echo "Bad: " . $array['$i'] . "\n";
echo "Good: " . $array[$i] . "\n";
echo "Bad: {$array['$i']}\n";
echo "Good: {$array[$i]}\n";
}
?>ΠΠΎΠ΄Π°Π½ΠΈΠΉ Π²ΠΈΡΠ΅ ΠΏΡΠΈΠΊΠ»Π°Π΄ Π²ΠΈΠ²Π΅Π΄Π΅:
Checking 0: Notice: Undefined index: $i in /path/to/script.html on line 9 Bad: Good: 1 Notice: Undefined index: $i in /path/to/script.html on line 11 Bad: Good: 1 Checking 1: Notice: Undefined index: $i in /path/to/script.html on line 9 Bad: Good: 2 Notice: Undefined index: $i in /path/to/script.html on line 11 Bad: Good: 2
More examples to demonstrate this behaviour:
<?php
// Show all errors
error_reporting(E_ALL);
$arr = array('fruit' => 'apple', 'veggie' => 'carrot');
// Correct
print $arr['fruit']; // apple
print $arr['veggie']; // carrot
// Incorrect. This works but also throws a PHP error of level E_NOTICE because
// of an undefined constant named fruit
//
// Notice: Use of undefined constant fruit - assumed 'fruit' in...
print $arr[fruit]; // apple
// This defines a constant to demonstrate what's going on. The value 'veggie'
// is assigned to a constant named fruit.
define('fruit', 'veggie');
// Notice the difference now
print $arr['fruit']; // apple
print $arr[fruit]; // carrot
// The following is okay, as it's inside a string. Constants are not looked for
// within strings, so no E_NOTICE occurs here
print "Hello $arr[fruit]"; // Hello apple
// With one exception: braces surrounding arrays within strings allows constants
// to be interpreted
print "Hello {$arr[fruit]}"; // Hello carrot
print "Hello {$arr['fruit']}"; // Hello apple
// This will not work, and will result in a parse error, such as:
// Parse error: parse error, expecting T_STRING' or T_VARIABLE' or T_NUM_STRING'
// This of course applies to using superglobals in strings as well
print "Hello $arr['fruit']";
print "Hello $_GET['foo']";
// Concatenation is another option
print "Hello " . $arr['fruit']; // Hello apple
?>
When error_reporting is set to
show E_NOTICE level errors (by setting it to
E_ALL, for example), such uses will become immediately
visible. By default,
error_reporting is set not to
show notices.
As stated in the syntax
section, what's inside the square brackets ('[' and
']') must be an expression. This means that code like
this works:
<?php
echo $arr[somefunc($bar)];
?>This is an example of using a function return value as the array index. PHP also knows about constants:
<?php
$error_descriptions[E_ERROR] = "A fatal error has occurred";
$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:
<?php
$error_descriptions[1] = "A fatal error has occurred";
$error_descriptions[2] = "PHP issued a warning";
$error_descriptions[8] = "This is just an informal notice";
?>
because E_ERROR equals 1, etc.
At some point in the future, the PHP team might want to add another
constant or keyword, or a constant in other code may interfere. For
example, it is already wrong to use the words empty and
default this way, since they are
reserved keywords.
ΠΠ°ΡΠ²Π°ΠΆΠ΅Π½Π½Ρ: To reiterate, inside a double-quoted string, it's valid to not surround array indexes with quotes so
"$foo[bar]"is valid. See the above examples for details on why as well as the section on variable parsing in strings.
For any of the types int, float,
string, bool and resource,
converting a value to an array results in an array with a single
element with index zero and the value of the scalar which was converted. In
other words, (array) $scalarValue is exactly the same as
array($scalarValue).
If an object is converted to an array, the result
is an array whose elements are the object's
properties. The keys are the member variable names, with a few notable
exceptions: integer properties are unaccessible;
private variables have the class name prepended to the variable
name; protected variables have a '*' prepended to the variable name. These
prepended values have NUL bytes on either side.
Uninitialized typed properties
are silently discarded.
<?php
class A {
private $B;
protected $C;
public $D;
function __construct()
{
$this->{1} = null;
}
}
var_export((array) new A());
?>ΠΠΎΠ΄Π°Π½ΠΈΠΉ Π²ΠΈΡΠ΅ ΠΏΡΠΈΠΊΠ»Π°Π΄ Π²ΠΈΠ²Π΅Π΄Π΅:
array ( '' . "\0" . 'A' . "\0" . 'B' => NULL, '' . "\0" . '*' . "\0" . 'C' => NULL, 'D' => NULL, 1 => NULL, )
These NUL can result in some unexpected behaviour:
<?php
class A {
private $A; // This will become '\0A\0A'
}
class B extends A {
private $A; // This will become '\0B\0A'
public $AA; // This will become 'AA'
}
var_dump((array) new B());
?>ΠΠΎΠ΄Π°Π½ΠΈΠΉ Π²ΠΈΡΠ΅ ΠΏΡΠΈΠΊΠ»Π°Π΄ Π²ΠΈΠ²Π΅Π΄Π΅:
array(3) {
["BA"]=>
NULL
["AA"]=>
NULL
["AA"]=>
NULL
}
The above will appear to have two keys named 'AA', although one of them is actually named '\0A\0A'.
It is possible to compare arrays with the array_diff() function and with array operators.
An array prefixed by ... will be expanded in place during array definition.
Only arrays and objects which implement Traversable can be expanded.
Array unpacking with ... is available as of PHP 7.4.0.
It's possible to expand multiple times, and add normal elements before or after the ... operator:
ΠΡΠΈΠΊΠ»Π°Π΄ #9 Simple array unpacking
<?php
// Using short array syntax.
// Also, works with array() syntax.
$arr1 = [1, 2, 3];
$arr2 = [...$arr1]; //[1, 2, 3]
$arr3 = [0, ...$arr1]; //[0, 1, 2, 3]
$arr4 = [...$arr1, ...$arr2, 111]; //[1, 2, 3, 1, 2, 3, 111]
$arr5 = [...$arr1, ...$arr1]; //[1, 2, 3, 1, 2, 3]
function getArr() {
return ['a', 'b'];
}
$arr6 = [...getArr(), 'c' => 'd']; //['a', 'b', 'c' => 'd']
?>
Unpacking an array with the ... operator follows the semantics of the array_merge() function.
That is, later string keys overwrite earlier ones and integer keys are renumbered:
ΠΡΠΈΠΊΠ»Π°Π΄ #10 Array unpacking with duplicate key
<?php
// string key
$arr1 = ["a" => 1];
$arr2 = ["a" => 2];
$arr3 = ["a" => 0, ...$arr1, ...$arr2];
var_dump($arr3); // ["a" => 2]
// integer key
$arr4 = [1, 2, 3];
$arr5 = [4, 5, 6];
$arr6 = [...$arr4, ...$arr5];
var_dump($arr6); // [1, 2, 3, 4, 5, 6]
// Which is [0 => 1, 1 => 2, 2 => 3, 3 => 4, 4 => 5, 5 => 6]
// where the original integer keys have not been retained.
?>ΠΠ°ΡΠ²Π°ΠΆΠ΅Π½Π½Ρ:
Keys that are neither integers nor strings throw a TypeError. Such keys can only be generated by a Traversable object.
ΠΠ°ΡΠ²Π°ΠΆΠ΅Π½Π½Ρ:
Prior to PHP 8.1, unpacking an array which has a string key is not supported:
<?php
$arr1 = [1, 2, 3];
$arr2 = ['a' => 4];
$arr3 = [...$arr1, ...$arr2];
// Fatal error: Uncaught Error: Cannot unpack array with string keys in example.php:5
$arr4 = [1, 2, 3];
$arr5 = [4, 5];
$arr6 = [...$arr4, ...$arr5]; // works. [1, 2, 3, 4, 5]
?>
The array type in PHP is very versatile. Here are some examples:
<?php
// This:
$a = array( 'color' => 'red',
'taste' => 'sweet',
'shape' => 'round',
'name' => 'apple',
4 // key will be 0
);
$b = array('a', 'b', 'c');
// . . .is completely equivalent with this:
$a = array();
$a['color'] = 'red';
$a['taste'] = 'sweet';
$a['shape'] = 'round';
$a['name'] = 'apple';
$a[] = 4; // key will be 0
$b = array();
$b[] = 'a';
$b[] = 'b';
$b[] = 'c';
// After the above code is executed, $a will be the array
// array('color' => 'red', 'taste' => 'sweet', 'shape' => 'round',
// 'name' => 'apple', 0 => 4), and $b will be the array
// array(0 => 'a', 1 => 'b', 2 => 'c'), or simply array('a', 'b', 'c').
?>ΠΡΠΈΠΊΠ»Π°Π΄ #11 Using array()
<?php
// 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();
?>ΠΡΠΈΠΊΠ»Π°Π΄ #12 Collection
<?php
$colors = array('red', 'blue', 'green', 'yellow');
foreach ($colors as $color) {
echo "Do you like $color?\n";
}
?>ΠΠΎΠ΄Π°Π½ΠΈΠΉ Π²ΠΈΡΠ΅ ΠΏΡΠΈΠΊΠ»Π°Π΄ Π²ΠΈΠ²Π΅Π΄Π΅:
Do you like red? Do you like blue? Do you like green? Do you like yellow?
Changing the values of the array directly is possible by passing them by reference.
ΠΡΠΈΠΊΠ»Π°Π΄ #13 Changing element in the loop
<?php
foreach ($colors as &$color) {
$color = mb_strtoupper($color);
}
unset($color); /* ensure that following writes to
$color will not modify the last array element */
print_r($colors);
?>ΠΠΎΠ΄Π°Π½ΠΈΠΉ Π²ΠΈΡΠ΅ ΠΏΡΠΈΠΊΠ»Π°Π΄ Π²ΠΈΠ²Π΅Π΄Π΅:
Array
(
[0] => RED
[1] => BLUE
[2] => GREEN
[3] => YELLOW
)
This example creates a one-based array.
ΠΡΠΈΠΊΠ»Π°Π΄ #14 One-based index
<?php
$firstquarter = array(1 => 'January', 'February', 'March');
print_r($firstquarter);
?>ΠΠΎΠ΄Π°Π½ΠΈΠΉ Π²ΠΈΡΠ΅ ΠΏΡΠΈΠΊΠ»Π°Π΄ Π²ΠΈΠ²Π΅Π΄Π΅:
Array
(
[1] => 'January'
[2] => 'February'
[3] => 'March'
)
ΠΡΠΈΠΊΠ»Π°Π΄ #15 Filling an array
<?php
// fill an array with all items from a directory
$handle = opendir('.');
while (false !== ($file = readdir($handle))) {
$files[] = $file;
}
closedir($handle);
?>Arrays are ordered. The order can be changed using various sorting functions. See the array functions section for more information. The count() function can be used to count the number of items in an array.
ΠΡΠΈΠΊΠ»Π°Π΄ #16 Sorting an array
<?php
sort($files);
print_r($files);
?>Because the value of an array can be anything, it can also be another array. This enables the creation of recursive and multi-dimensional arrays.
ΠΡΠΈΠΊΠ»Π°Π΄ #17 Recursive and multi-dimensional arrays
<?php
$fruits = array ( "fruits" => array ( "a" => "orange",
"b" => "banana",
"c" => "apple"
),
"numbers" => array ( 1,
2,
3,
4,
5,
6
),
"holes" => array ( "first",
5 => "second",
"third"
)
);
// Some examples to address values in the array above
echo $fruits["holes"][5]; // prints "second"
echo $fruits["fruits"]["a"]; // prints "orange"
unset($fruits["holes"][0]); // remove "first"
// Create a new multi-dimensional array
$juices["apple"]["green"] = "good";
?>Array assignment always involves value copying. Use the reference operator to copy an array by reference.
<?php
$arr1 = array(2, 3);
$arr2 = $arr1;
$arr2[] = 4; // $arr2 is changed,
// $arr1 is still array(2, 3)
$arr3 = &$arr1;
$arr3[] = 4; // now $arr1 and $arr3 are the same
?>please note that when arrays are copied, the "reference status" of their members is preserved (http://www.php.net/manual/en/language.references.whatdo.php).I think your first, main example is needlessly confusing, very confusing to newbies:
$array = array(
"foo" => "bar",
"bar" => "foo",
);
It should be removed.
For newbies:
An array index can be any string value, even a value that is also a value in the array.
The value of array["foo"] is "bar".
The value of array["bar"] is "foo"
The following expressions are both true:
$array["foo"] == "bar"
$array["bar"] == "foo""If you convert a NULL value to an array, you get an empty array."
This turns out to be a useful property. Say you have a search function that returns an array of values on success or NULL if nothing found.
<?php $values = search(...); ?>
Now you want to merge the array with another array. What do we do if $values is NULL? No problem:
<?php $combined = array_merge((array)$values, $other); ?>
Voila.Beware that if you're using strings as indices in the $_POST array, that periods are transformed into underscores:
<html>
<body>
<?php
printf("POST: "); print_r($_POST); printf("<br/>");
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="Windows3.1" value="Sux">
<input type="submit" value="Click" />
</form>
</body>
</html>
Once you click on the button, the page displays the following:
POST: Array ( [Windows3_1] => Sux )Note that array value buckets are reference-safe, even through serialization.
<?php
$x='initial';
$test=array('A'=>&$x,'B'=>&$x);
$test=unserialize(serialize($test));
$test['A']='changed';
echo $test['B']; // Outputs "changed"
?>
This can be useful in some cases, for example saving RAM within complex structures.