索引數組php
D:\php20190701\php_code\0807\array.php:10:
array (size=3)
0 => string 'first' (length=5)
1 => string 'second' (length=6)
2 => string 'third' (length=5)
複製代碼
關聯數組數組
D:\php20190701\php_code\0807\array.php:10:
array (size=3)
'one' => string 'first' (length=5)
'two' => string 'second' (length=6)
'three' => string 'third' (length=5)
複製代碼
注意bash
<?php
$arr = array(
'one'=>'first',
'two'=>'second',
'three'=>'third'
);
var_dump($arr);
複製代碼
<?php
$arr = array(); // 空數組
$arr1 = []; // 空數組
複製代碼
<?php
$arr = array(
'first'=>123,
'second'=>'hello',
'third'=>array(
'hello',
'world'
),
'forth'=>array(
'one'=>'hello',
'two'=>'world'
)
);
複製代碼
經過[]的形式定義數組,代替array()spa
注意code
檢測是否爲數組對象
數組鍵名索引
<?php
$arr = [
1=>'first',
'1'=>'second' // 會覆蓋
];
var_dump($arr);
複製代碼
<?php
$arr = [
1.1=>'first',
2.2=>'second',
2.3=>'third', // 會把second覆蓋掉
];
var_dump($arr);
複製代碼
D:\php20190701\php_code\0807\array2.php:10:
array (size=2)
1 => string 'first' (length=5)
2 => string 'second' (length=6)
複製代碼
<?php
$arr = [
true=>'first',
false=>'second'
];
var_dump($arr);
複製代碼
D:\php20190701\php_code\0807\array2.php:9:
array (size=2)
1 => string 'first' (length=5)
0 => string 'second' (length=6)
複製代碼
<?php
$arr = [
null=>'first', // 會轉成空字符串
'1'=>'second'
];
var_dump($arr);
var_dump($arr['']); // 能夠使用空字符串來取值
複製代碼
<?php
$arr = [
1=>'first', // 會轉成空字符串
11=>'second',
'three'=>'third'
];
$arr[] = "forth";
var_dump($arr);
複製代碼
<?php
$arr = [
-2=>'first', // 會轉成空字符串
-11=>'second',
'three'=>'third'
];
$arr[] = "forth";
var_dump($arr);
複製代碼
D:\php20190701\php_code\0807\array2.php:13:
array (size=4)
-2 => string 'first' (length=5)
-11 => string 'second' (length=6)
'three' => string 'third' (length=5)
0 => string 'forth' (length=5)
複製代碼
<?php
$arr = [];
$arr[] = 'first';
$arr[] = 'second';
$arr[] = 'third';
$arr[] = 'forth';
$arr[] = 'fifth';
$arr[] = 'sixth';
$arr[] = 'seventh';
var_dump($arr);
複製代碼
D:\php20190701\php_code\0807\array2.php:13:
array (size=7)
0 => string 'first' (length=5)
1 => string 'second' (length=6)
2 => string 'third' (length=5)
3 => string 'forth' (length=5)
4 => string 'fifth' (length=5)
5 => string 'sixth' (length=5)
6 => string 'seventh' (length=7)
複製代碼
<?php
$arr = [];
$arr['one'] = 'first';
$arr['two'] = 'second';
$arr['three'] = 'third';
$arr[] = 'forth';
$arr[] = 'fifth';
$arr[] = 'sixth';
$arr['seven'] = 'seventh';
var_dump($arr);
複製代碼
D:\php20190701\php_code\0807\array2.php:13:
array (size=7)
'one' => string 'first' (length=5)
'two' => string 'second' (length=6)
'three' => string 'third' (length=5)
0 => string 'forth' (length=5)
1 => string 'fifth' (length=5)
2 => string 'sixth' (length=5)
'seven' => string 'seventh' (length=7)
複製代碼
<?php
var_dump(range(1,10,1));
複製代碼
D:\php20190701\php_code\0807\array2.php:5:
array (size=10)
0 => int 1
1 => int 2
2 => int 3
3 => int 4
4 => int 5
5 => int 6
6 => int 7
7 => int 8
8 => int 9
9 => int 10
複製代碼
<?php
$username = "zhangsan";
$age = 18;
$sex = 'male';
$userinfo = compact('username','age','sex');
var_dump($userinfo);
複製代碼
<?php
define('HELLO',['hello','world']);
var_dump(HELLO);
HELLO[0] = "!!!"; // 報錯
複製代碼
<?php
$arr = ['hello','world'];
echo $arr{0};
複製代碼
[0=>null]
<?php
$arr1 = [
'hello','world'
];
$arr2 = [
'hello','php'
];
var_dump($arr1+$arr2);
複製代碼
D:\php20190701\php_code\0807\array_op.php:11:
array (size=2)
0 => string 'hello' (length=5)
1 => string 'world' (length=5)
複製代碼
<?php
$arr1 = [
0=>'1',
1=>'world'
];
$arr2 = [
"0"=>1,
"1"=>'world'
];
var_dump($arr1 == $arr2); // true, 不比較順序
複製代碼
<?php
$arr1 = [
0=>'1',
1=>'world',
];
$arr2 = [
"0"=>'1',
"1"=>'world',
];
var_dump($arr1 === $arr2); // 下標, 值的類型, 順序都必須同樣
複製代碼