Array 锟斤拷锟斤拷

PHP 锟叫碉拷锟斤拷锟斤拷实锟斤拷锟斤拷锟斤拷一锟斤拷锟斤拷锟斤拷映锟戒。映锟斤拷锟斤拷一锟街帮拷 values 锟斤拷锟斤拷锟斤拷 keys 锟斤拷锟斤拷锟酵★拷锟斤拷锟斤拷锟斤拷锟节很多方锟斤拷锟斤拷锟斤拷锟脚伙拷锟斤拷锟斤拷丝锟斤拷园锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷椋拷锟斤拷斜锟斤拷锟斤拷锟斤拷锟斤拷锟缴拷斜锟斤拷锟接筹拷锟斤拷一锟斤拷实锟街o拷锟斤拷锟街典,锟斤拷锟较o拷栈锟斤拷锟斤拷锟斤拷锟皆硷拷锟斤拷锟斤拷锟斤拷锟斤拷浴锟斤拷锟斤拷锟斤拷锟斤拷锟皆拷氐锟街狄诧拷锟斤拷锟斤拷锟斤拷锟揭伙拷锟斤拷锟斤拷椋拷锟斤拷谓峁癸拷投锟轿拷锟斤拷锟揭诧拷锟斤拷锟斤拷锟侥★拷

锟斤拷锟斤拷锟斤拷些锟结构锟斤拷锟斤拷锟剿憋拷锟街诧拷姆锟轿э拷锟斤拷锟斤拷锟斤拷锟矫匡拷纸峁癸拷锟斤拷倩锟斤拷峁┮伙拷锟斤拷锟斤拷印锟揭拷玫锟斤拷锟叫╋拷峁癸拷母锟斤拷锟斤拷锟较拷锟斤拷锟斤拷锟轿匡拷锟叫关此癸拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟�

锟斤法

锟斤拷锟斤拷锟斤拷锟斤拷 array()

锟斤拷锟斤拷锟斤拷 array() 锟斤拷锟皆结构锟斤拷锟铰斤拷一锟斤拷锟斤拷锟介。锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟矫讹拷锟脚分革拷锟斤拷 锟斤拷锟斤拷key锟斤拷 => 值锟斤拷value锟斤拷锟皆★拷

array(  key =>  value
     , ...
     )
// 锟斤拷锟斤拷key锟斤拷锟斤拷锟斤拷锟斤拷一锟斤拷锟斤拷锟斤拷 integer 锟斤拷锟街凤拷锟斤拷 string
// 值锟斤拷value锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟酵碉拷值

锟斤拷锟揭伙拷锟斤拷锟斤拷榈ピ拷锟侥讹拷锟脚匡拷锟斤拷省锟皆★拷通锟斤拷锟斤拷锟节碉拷锟斤拷锟斤拷锟介定锟斤拷锟叫o拷锟斤拷锟界常锟斤拷 array(1, 2) 锟斤拷锟斤拷锟斤拷 array(1, 2, )锟斤拷锟皆讹拷锟斤拷锟斤拷锟介定锟斤拷通锟斤拷锟斤拷锟斤拷锟斤拷锟揭伙拷锟斤拷锟斤拷牛锟斤拷锟斤拷锟揭拷锟斤拷一锟斤拷锟铰碉拷元时锟斤拷锟斤拷锟姐。

锟斤拷 5.4 锟斤拷锟斤拷锟绞癸拷枚锟斤拷锟斤拷槎拷锟斤拷锓拷锟斤拷锟� [] 锟斤拷锟� array()锟斤拷

Example #1 一锟斤拷锟斤拷锟斤拷锟斤拷

<?php
$array 
= array(
    
"foo" => "bar",
    
"bar" => "foo",
);

// 锟斤拷 PHP 5.4 锟斤拷
$array = [
    
"foo" => "bar",
    
"bar" => "foo",
];
?>

key 锟斤拷锟斤拷锟斤拷 integer 锟斤拷锟斤拷 string锟斤拷value 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟酵★拷

锟斤拷锟斤拷 key 锟斤拷锟斤拷锟斤拷锟铰碉拷强锟斤拷转锟斤拷锟斤拷

  • 锟斤拷锟斤拷锟叫合凤拷锟斤拷锟斤拷值锟斤拷锟街凤拷锟斤拷锟结被转锟斤拷为锟斤拷锟酵★拷锟斤拷锟斤拷锟斤拷锟� "8" 实锟绞会被锟斤拷锟斤拷为 8锟斤拷锟斤拷锟斤拷 "08" 锟津不伙拷强锟斤拷转锟斤拷锟斤拷锟斤拷为锟戒不锟斤拷一锟斤拷锟较凤拷锟斤拷十锟斤拷锟斤拷锟斤拷值锟斤拷
  • 锟斤拷锟斤拷锟斤拷也锟结被转锟斤拷为锟斤拷锟酵o拷锟斤拷味锟斤拷锟斤拷小锟斤拷锟斤拷锟街会被锟斤拷去锟斤拷锟斤拷锟斤拷锟斤拷锟� 8.7 实锟绞会被锟斤拷锟斤拷为 8锟斤拷
  • 锟斤拷锟斤拷值也锟结被转锟斤拷锟斤拷锟斤拷锟酵★拷锟斤拷锟斤拷锟斤拷 true 实锟绞会被锟斤拷锟斤拷为 1 锟斤拷锟斤拷锟斤拷 false 锟结被锟斤拷锟斤拷为 0锟斤拷
  • Null 锟结被转锟斤拷为锟斤拷锟街凤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷 null 实锟绞会被锟斤拷锟斤拷为 ""锟斤拷
  • 锟斤拷锟斤拷投锟斤拷锟�锟斤拷锟斤拷锟斤拷锟斤拷为锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟矫达拷锟斤拷岬硷拷戮锟斤拷妫�Illegal offset type锟斤拷

锟斤拷锟斤拷锟斤拷锟斤拷槎拷锟斤拷卸锟斤拷锟斤拷元锟斤拷使锟斤拷锟斤拷同一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷只使锟斤拷锟斤拷锟斤拷锟揭伙拷锟斤拷锟街帮拷亩锟斤拷锟斤拷锟斤拷锟斤拷恕锟�

Example #2 锟斤拷锟斤拷强锟斤拷锟诫覆锟斤拷示锟斤拷

<?php
$array 
= array(
    
1    => "a",
    
"1"  => "b",
    
1.5  => "c",
    
true => "d",
);
var_dump($array);
?>

锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�

array(1) {
  [1]=>
  string(1) "d"
}

锟斤拷锟斤拷锟斤拷锟斤拷锟叫的硷拷锟斤拷锟斤拷锟斤拷强锟斤拷转锟斤拷为 1锟斤拷锟斤拷每一锟斤拷锟铰碉拷元锟斤拷锟结覆锟斤拷前一锟斤拷锟斤拷值锟斤拷锟斤拷锟绞o拷碌锟街伙拷锟揭伙拷锟� "d"锟斤拷

PHP 锟斤拷锟斤拷锟斤拷锟酵憋拷锟斤拷锟� integer 锟斤拷 string 锟斤拷锟酵的硷拷锟斤拷锟斤拷锟斤拷为 PHP 实锟绞诧拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷凸锟斤拷锟斤拷锟斤拷椤�

锟斤拷锟斤拷愿锟斤拷锟斤拷锟街得伙拷锟街革拷锟斤拷锟斤拷锟斤拷锟斤拷锟饺★拷锟角帮拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟街碉拷锟斤拷锟斤拷碌募锟斤拷锟斤拷锟斤拷歉锟街碉拷锟揭伙拷锟斤拷锟斤拷指锟斤拷锟侥硷拷锟斤拷锟窖撅拷锟斤拷锟斤拷值锟斤拷锟斤拷锟街碉拷岜伙拷锟斤拷恰锟�

Example #3 锟斤拷锟� integer 锟斤拷 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 为锟斤拷选锟筋。锟斤拷锟轿粗革拷锟斤拷锟絇HP 锟斤拷锟皆讹拷使锟斤拷之前锟矫癸拷锟斤拷锟斤拷锟� integer 锟斤拷锟斤拷锟斤拷锟斤拷 1 锟斤拷为锟铰的硷拷锟斤拷锟斤拷

Example #4 没锟叫硷拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷

<?php
$array 
= array("foo""bar""hallo""world");
var_dump($array);
?>

锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�

array(4) {
  [0]=>
  string(3) "foo"
  [1]=>
  string(3) "bar"
  [2]=>
  string(5) "hallo"
  [3]=>
  string(5) "world"
}

锟斤拷锟斤拷锟斤拷只锟斤拷某些锟斤拷元指锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟侥匡拷锟矫o拷

Example #5 锟斤拷锟皆诧拷锟街碉拷元指锟斤拷锟斤拷锟斤拷

<?php
$array 
= array(
         
"a",
         
"b",
    
=> "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锟斤拷

锟矫凤拷锟斤拷锟斤拷锟斤法锟斤拷锟斤拷锟斤拷锟介单元

锟斤拷锟介单元锟斤拷锟斤拷通锟斤拷 array[key] 锟斤法锟斤拷锟斤拷锟绞★拷

Example #6 锟斤拷锟斤拷锟斤拷锟介单元

<?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"

Note:

锟斤拷锟斤拷锟脚和伙拷锟斤拷锟脚匡拷锟皆伙拷锟斤拷使锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟介单元锟斤拷锟斤拷锟斤拷 $array[42] 锟斤拷 $array{42} 锟斤拷锟斤拷锟斤拷锟斤拷效锟斤拷锟斤拷同锟斤拷锟斤拷

锟斤拷 PHP 5.4 锟斤拷锟斤拷锟斤拷锟街憋拷佣院锟斤拷锟斤拷蚍椒锟斤拷锟斤拷玫慕锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷茫锟斤拷诖锟街爸伙拷锟酵拷锟揭伙拷锟斤拷锟绞憋拷锟斤拷锟斤拷锟�

锟斤拷 PHP 5.5 锟斤拷锟斤拷锟街憋拷佣锟揭伙拷锟斤拷锟斤拷锟皆拷徒锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟矫★拷

Example #7 锟斤拷锟斤拷锟斤拷锟斤拷锟�

<?php
function getArray() {
    return array(
123);
}

// on PHP 5.4
$secondElement getArray()[1];

// previously
$tmp getArray();
$secondElement $tmp[1];

// or
list(, $secondElement) = getArray();
?>

Note:

锟斤拷图锟斤拷锟斤拷一锟斤拷未锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷魏锟轿达拷锟斤拷锟斤拷锟斤拷一锟斤拷锟斤拷锟结导锟斤拷 E_NOTICE 锟斤拷锟斤拷锟斤拷锟斤拷锟较拷锟斤拷锟斤拷锟轿� NULL锟斤拷

锟矫凤拷锟斤拷锟脚碉拷锟斤法锟铰斤拷锟斤拷锟睫革拷

锟斤拷锟斤拷通锟斤拷锟斤拷示锟斤拷锟借定锟斤拷锟叫碉拷值锟斤拷锟睫革拷一锟斤拷锟斤拷锟斤拷锟斤拷锟介。

锟斤拷锟斤拷通锟斤拷锟节凤拷锟斤拷锟斤拷锟斤拷指锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟介赋值实锟街的★拷也锟斤拷锟斤拷省锟皆硷拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷赂锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟揭伙拷钥盏姆锟斤拷锟斤拷牛锟�[]锟斤拷锟斤拷

$arr[key] = value;
$arr[] = value;
// key 锟斤拷锟斤拷锟斤拷 integer 锟斤拷 string
// value 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟酵碉拷值

锟斤拷锟� $arr 锟斤拷锟斤拷锟斤拷锟节o拷锟斤拷锟斤拷锟铰斤拷一锟斤拷锟斤拷锟斤拷也锟斤拷锟斤拷一锟斤拷锟铰斤拷锟斤拷锟斤拷姆锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟轿拷锟斤拷 $arr 锟窖撅拷锟斤拷锟斤拷锟斤拷值锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟� string锟斤拷锟斤拷锟街碉拷岜o拷锟斤拷锟� [] 实锟斤拷锟较达拷锟斤拷锟斤拷锟街凤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟�锟斤拷锟斤拷始锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷梅锟绞斤拷锟街憋拷痈锟斤拷涓持碉拷锟斤拷锟�

要锟睫革拷某锟斤拷值锟斤拷通锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷玫锟皆拷锟揭伙拷锟斤拷锟街碉拷锟揭撅拷锟侥筹拷锟街碉拷裕锟斤拷锟斤拷锟斤拷锟斤拷 unset() 锟斤拷锟斤拷锟斤拷

<?php
$arr 
= array(=> 112 => 2);

$arr[] = 56;    // This is the same as $arr[13] = 56;
                // at this point of the script

$arr["x"] = 42// This adds a new element to
                // the array with key "x"
                
unset($arr[5]); // This removes the element from the array

unset($arr);    // This deletes the whole array
?>

Note:

锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷诺锟矫伙拷锟街革拷锟斤拷锟斤拷锟斤拷锟斤拷锟饺★拷锟角帮拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷值锟斤拷锟铰的硷拷锟斤拷锟斤拷锟角革拷值锟斤拷锟斤拷 1锟斤拷锟斤拷锟斤拷锟斤拷小为 0锟斤拷锟斤拷锟斤拷锟斤拷锟角帮拷锟矫伙拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷为 0锟斤拷

注锟斤拷锟斤拷锟斤拷锟斤拷使锟矫碉拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟�锟斤拷一锟斤拷锟斤拷前锟斤拷锟斤拷锟斤拷锟斤拷锟叫★拷锟斤拷只要锟斤拷锟较达拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟节癸拷锟斤拷锟斤拷锟剿★拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟剿碉拷锟斤拷锟�

<?php
// 锟斤拷锟斤拷一锟斤拷锟津单碉拷锟斤拷锟斤拷
$array = array(12345);
print_r($array);

// 锟斤拷锟斤拷删锟斤拷锟斤拷锟叫碉拷锟斤拷锟斤拷元锟截o拷锟斤拷锟斤拷锟斤拷锟斤拷锟介本锟斤拷锟斤拷:
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
)

实锟矫猴拷锟斤拷

锟叫很讹拷锟斤拷锟斤拷锟斤拷锟侥猴拷锟斤拷锟斤拷锟轿硷拷锟斤拷锟介函锟斤拷一锟节★拷

Note:

unset() 锟斤拷锟斤拷锟斤拷锟斤拷删锟斤拷锟斤拷锟斤拷锟叫碉拷某锟斤拷锟斤拷锟斤拷锟斤拷要注锟斤拷锟斤拷锟介将锟斤拷锟斤拷锟截斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟揭撅拷锟斤拷锟斤拷亟锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟� array_values() 锟斤拷锟斤拷锟斤拷

<?php
$a 
= array(=> 'one'=> 'two'=> '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');
*/

$b array_values($a);
// Now $b is array(0 => 'one', 1 =>'three')
?>

foreach 锟斤拷锟狡结构锟斤拷专锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷摹锟斤拷锟斤拷峁╋拷锟揭伙拷锟斤拷虻サ姆锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷椤�

锟斤拷锟斤拷锟斤拷什么锟酵诧拷锟斤拷什么

为什么 $foo[bar] 锟斤拷锟剿o拷

应锟斤拷始锟斤拷锟斤拷锟斤拷锟街凤拷锟斤拷锟斤拷示锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟较硷拷锟斤拷锟斤拷锟脚★拷锟斤拷锟斤拷锟斤拷 $foo['bar'] 锟斤拷锟斤拷锟斤拷 $foo[bar]锟斤拷锟斤拷锟斤拷为什么锟截o拷锟斤拷锟斤拷锟斤拷锟较的脚憋拷锟叫硷拷锟斤拷锟斤拷锟斤拷锟斤法锟斤拷

<?php
$foo
[bar] = 'enemy';
echo 
$foo[bar];
// etc
?>

锟斤拷锟斤拷锟角达拷模锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷小锟斤拷锟矫次裁达拷锟斤拷锟斤拷兀锟皆拷锟斤拷谴舜锟斤拷锟斤拷锟斤拷锟揭伙拷锟轿达拷锟斤拷锟侥筹拷锟斤拷锟斤拷bar锟斤拷锟斤拷锟斤拷锟斤拷锟街凤拷锟斤拷锟斤拷'bar'锟斤拷注锟斤拷锟斤拷锟脚o拷锟斤拷锟斤拷 PHP 锟斤拷锟杰伙拷锟斤拷锟皆猴拷锟斤拷顺锟斤拷锟斤拷锟斤拷锟斤拷业锟斤拷锟斤拷锟侥达拷锟斤拷锟斤拷锟斤拷同锟斤拷锟斤拷锟斤拷锟街★拷锟斤拷锟斤拷锟斤拷锟叫o拷锟斤拷锟斤拷为 PHP 锟皆讹拷锟斤拷锟斤拷锟街凤拷锟斤拷锟斤拷没锟斤拷锟斤拷锟脚碉拷锟街凤拷锟斤拷锟揭诧拷锟斤拷应锟斤拷锟轿猴拷锟斤拷知锟斤拷锟脚o拷转锟斤拷锟斤拷一锟斤拷锟斤拷值为锟斤拷锟斤拷锟街凤拷锟斤拷锟斤拷锟斤拷锟斤拷锟街凤拷锟斤拷锟斤拷锟斤拷锟界,锟斤拷锟矫伙拷谐锟斤拷锟斤拷锟斤拷锟轿� bar锟斤拷PHP 锟斤拷锟斤拷锟斤拷锟斤拷锟轿� 'bar' 锟斤拷使锟斤拷之锟斤拷

Note: 锟解并锟斤拷锟斤拷味锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟脚★拷锟矫诧拷锟脚革拷锟斤拷锟斤拷为锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟侥硷拷锟斤拷锟斤拷锟脚o拷锟斤拷锟斤拷锟绞� PHP 锟斤拷锟杰斤拷锟斤拷锟斤拷锟角★拷

<?php
error_reporting
(E_ALL);
ini_set('display_errors'true);
ini_set('html_errors'false);
// Simple array:
$array = array(12);
$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

锟斤拷示锟斤拷锟斤拷为锟侥革拷锟斤拷锟斤拷锟接o拷

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

锟斤拷锟斤拷 error_reporting 锟斤拷锟斤拷示 E_NOTICE 锟斤拷锟斤拷拇锟斤拷螅ń锟斤拷锟斤拷锟轿� E_ALL锟斤拷时锟斤拷锟斤拷锟斤拷锟斤拷些锟斤拷锟斤拷默锟斤拷锟斤拷锟斤拷锟� error_reporting 锟斤拷锟截闭诧拷锟斤拷示锟斤拷些锟斤拷

锟斤拷锟斤拷锟斤法一锟斤拷锟叫规定锟斤拷一锟斤拷锟斤拷锟节凤拷锟斤拷锟脚o拷"["锟斤拷"]"锟斤拷之锟斤拷锟斤拷锟斤拷锟揭伙拷锟斤拷锟斤拷式锟斤拷锟斤拷锟斤拷味锟脚匡拷锟斤拷锟斤拷锟斤拷写锟斤拷

<?php
echo $arr[somefunc($bar)];
?>

锟斤拷锟斤拷一锟斤拷锟矫猴拷锟斤拷锟斤拷锟斤拷值锟斤拷为锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟接★拷PHP 也锟斤拷锟斤拷锟斤拷锟斤拷知锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷之前锟窖撅拷锟斤拷锟斤拷锟斤拷

<?php
$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";
?>

注锟斤拷 E_ERROR 也锟角革拷锟较凤拷锟侥憋拷识锟斤拷锟斤拷锟酵和碉拷一锟斤拷锟斤拷锟斤拷锟叫碉拷 bar 一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷一锟斤拷锟斤拷锟斤拷实锟斤拷锟较猴拷锟斤拷锟斤拷写锟斤拷锟斤拷一锟斤拷锟侥o拷

<?php
$error_descriptions
[1] = "A fatal error has occured";
$error_descriptions[2] = "PHP issued a warning";
$error_descriptions[8] = "This is just an informal notice";
?>

锟斤拷为 E_ERROR 锟斤拷锟斤拷 1锟斤拷锟饺等★拷

锟斤拷么为什么锟斤拷锟斤拷锟斤拷锟斤拷锟矫o拷

也锟斤拷锟斤拷一锟届,PHP 锟斤拷锟斤拷小锟斤拷锟斤拷芑锟斤拷锟斤拷锟斤拷锟揭伙拷锟斤拷锟斤拷锟斤拷锟斤拷吖丶锟斤拷郑锟斤拷锟斤拷锟斤拷没锟斤拷锟斤拷锟较o拷锟斤拷院锟斤拷锟斤拷约锟斤拷某锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷碌某锟斤拷锟斤拷锟斤拷蔷锟斤拷锟斤拷榉筹拷恕锟斤拷锟斤拷锟斤拷丫锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟� empty 锟斤拷 default 锟斤拷锟斤拷锟斤拷锟斤拷锟剿o拷锟斤拷为锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷

Note: 锟斤拷锟斤拷一锟轿o拷锟斤拷双锟斤拷锟斤拷锟街凤拷锟斤拷锟叫o拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟角合凤拷锟斤拷锟斤拷锟� "$foo[bar]" 锟角合凤拷锟侥o拷"锟较凤拷"锟斤拷原锟斤拷为 valid锟斤拷锟斤拷实锟绞诧拷锟斤拷锟叫o拷锟斤拷么锟斤拷确实锟斤拷锟皆凤拷锟斤拷锟斤拷锟斤拷母锟皆拷兀锟斤拷锟斤拷腔岜ㄒ伙拷锟斤拷锟斤拷锟轿达拷锟斤拷锟斤拷 notice锟斤拷锟斤拷锟斤拷锟斤拷危锟角匡拷医锟斤拷椴灰癸拷锟� $foo[bar]锟斤拷锟斤拷锟斤拷写锟斤拷锟斤拷锟斤拷要使锟斤拷 $foo['bar'] 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷元锟截★拷--haohappy 注锟斤拷锟斤拷锟斤拷锟斤拷为什么锟轿硷拷锟斤拷锟较碉拷锟斤拷锟接猴拷锟街凤拷锟斤拷锟叫的憋拷锟斤拷锟斤拷锟斤拷锟叫的斤拷锟酵★拷

转锟斤拷为锟斤拷锟斤拷

锟斤拷锟斤拷锟斤拷锟斤拷 integer锟斤拷float锟斤拷string锟斤拷boolean 锟斤拷 resource 锟斤拷锟酵o拷锟斤拷锟斤拷锟揭伙拷锟街底拷锟轿拷锟斤拷椋拷锟斤拷玫锟揭伙拷锟斤拷锟斤拷锟揭伙拷锟皆拷氐锟斤拷锟斤拷椋拷锟斤拷卤锟轿� 0锟斤拷锟斤拷元锟截硷拷为锟剿憋拷锟斤拷锟斤拷值锟斤拷锟斤拷锟戒话说锟斤拷(array)$scalarValue 锟斤拷 array($scalarValue) 锟斤拷全一锟斤拷锟斤拷

锟斤拷锟揭伙拷锟� object 锟斤拷锟斤拷转锟斤拷为 array锟斤拷锟斤拷锟斤拷为一锟斤拷锟斤拷锟介,锟戒单元为锟矫讹拷锟斤拷锟斤拷锟斤拷浴锟斤拷锟斤拷锟斤拷锟轿拷锟皆憋拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷屑锟斤拷锟斤拷锟斤拷猓猴拷锟斤拷锟斤拷锟斤拷圆锟斤拷煞锟斤拷剩锟剿斤拷斜锟斤拷锟角帮拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷前缀锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷前锟斤拷锟斤拷锟揭伙拷锟� '*' 锟斤拷前缀锟斤拷锟斤拷些前缀锟斤拷前锟襟都革拷锟斤拷一锟斤拷 NULL 锟街凤拷锟斤拷锟斤拷岬硷拷锟揭恍╋拷锟斤拷锟皆ぶ拷锟斤拷锟轿拷锟�

<?php

class {
    private 
$A// This will become '\0A\0A'
}

class 
extends {
    private 
$A// This will become '\0B\0A'
    
public $AA// This will become 'AA'
}

var_dump((array) new B());
?>

锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷为 'AA'锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷一锟斤拷实锟斤拷锟斤拷锟斤拷 '\0A\0A'锟斤拷

锟斤拷 NULL 转锟斤拷为 array 锟斤拷玫锟揭伙拷锟斤拷盏锟斤拷锟斤拷椤�

锟饺斤拷

锟斤拷锟斤拷锟斤拷 array_diff() 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟�锟斤拷锟饺斤拷锟斤拷锟介。

示锟斤拷

PHP 锟叫碉拷锟斤拷锟斤拷锟斤拷锟斤拷锟叫非筹拷锟斤拷锟斤拷锟酵撅拷锟斤拷锟斤拷锟斤拷锟揭恍┦撅拷锟斤拷锟�

<?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').
?>

Example #8 使锟斤拷 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();         
?>

Example #9 锟斤拷锟斤拷

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

直锟接改憋拷锟斤拷锟斤拷锟街碉拷锟� PHP 5 锟斤拷锟斤拷锟酵拷锟斤拷锟斤拷么锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟街帮拷陌姹撅拷锟揭拷锟揭拷锟饺★拷锟酵拷姆锟斤拷锟斤拷锟�

Example #10 锟斤拷循锟斤拷锟叫改变单元

<?php
// PHP 5
foreach ($colors as &$color) {
    
$color strtoupper($color);
}
unset(
$color); /* ensure that following writes to
$color will not modify the last array element */

// Workaround for older versions
foreach ($colors as $key => $color) {
    
$colors[$key] = strtoupper($color);
}

print_r($colors);
?>

锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�

Array
(
    [0] => RED
    [1] => BLUE
    [2] => GREEN
    [3] => YELLOW
)

锟斤拷锟斤拷锟斤拷锟斤拷一锟斤拷锟铰憋拷锟� 1 锟斤拷始锟斤拷锟斤拷锟介。

Example #11 锟铰憋拷锟� 1 锟斤拷始锟斤拷锟斤拷锟斤拷

<?php
$firstquarter  
= array(=> 'January''February''March');
print_r($firstquarter);
?>

锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�

Array 
(
    [1] => 'January'
    [2] => 'February'
    [3] => 'March'
)

Example #12 锟斤拷锟斤拷锟斤拷锟�

<?php
// fill an array with all items from a directory
$handle opendir('.');
while (
false !== ($file readdir($handle))) {
    
$files[] = $file;
}
closedir($handle); 
?>

锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷摹锟揭诧拷锟斤拷锟绞癸拷貌锟酵拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷谋锟剿筹拷颉8锟斤拷锟斤拷锟较拷渭锟�锟斤拷锟介函锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷 count() 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷元锟截的革拷锟斤拷锟斤拷

Example #13 锟斤拷锟斤拷锟斤拷锟斤拷

<?php
sort
($files);
print_r($files);
?>

锟斤拷为锟斤拷锟斤拷锟叫碉拷值锟斤拷锟斤拷为锟斤拷锟斤拷值锟斤拷也锟斤拷锟斤拷锟斤拷一锟斤拷锟斤拷锟介。锟斤拷锟斤拷锟斤拷锟皆诧拷锟斤拷锟捷癸拷锟斤拷维锟斤拷锟介。

Example #14 锟捷癸拷投锟轿拷锟斤拷锟�

<?php
$fruits 
= array ( "fruits"  => array ( "a" => "orange",
                                       
"b" => "banana",
                                       
"c" => "apple"
                                     
),
                  
"numbers" => array ( 1,
                                       
2,
                                       
3,
                                       
4,
                                       
5,
                                       
6
                                     
),
                  
"holes"   => array (      "first",
                                       
=> "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) 锟侥革拷值锟斤拷锟角伙拷锟芥及锟斤拷值锟侥匡拷锟斤拷锟斤拷使锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟�通锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟介。

<?php
$arr1 
= array(23);
$arr2 $arr1;
$arr2[] = 4// $arr2 is changed,
             // $arr1 is still array(2, 3)
             
$arr3 = &$arr1;
$arr3[] = 4// now $arr1 and $arr3 are the same
?>