Php數(shù)組操作

1.定義數(shù)組

<?php
$cars=array("Volvo","BMW","Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?> 

2.獲取數(shù)組長度

<?php
 $cars=array("Volvo","BMW","Toyota");
echo count($cars);
?> 

3.遍歷數(shù)組

<?php
 $cars=array("Volvo","BMW","Toyota");
 $arrlength=count($cars);

for($x=0;$x<$arrlength;$x++)
 {
 echo $cars[$x];
 echo "<br>";
 }
?> 

4.關(guān)聯(lián)數(shù)組,類似map

key 可以是 integer 或者 string。value 可以是任意類型

1.包含有合法整型值的字符串會被轉(zhuǎn)換為整型。例如鍵名 "8" 實際會被儲存為 8。
但是 "08" 則不會強制轉(zhuǎn)換,因為其不是一個合法的十進制數(shù)值。
2.浮點數(shù)也會被轉(zhuǎn)換為整型,意味著其小數(shù)部分會被舍去。例如鍵名 8.7 
實際會被儲存為 8。
3.布爾值也會被轉(zhuǎn)換成整型。即鍵名 true 實際會被儲存為 1 而鍵名 false 會被儲存為 0
4.Null 會被轉(zhuǎn)換為空字符串,即鍵名 null 實際會被儲存為 ""。
5.數(shù)組和對象不能被用為鍵名。堅持這么做會導(dǎo)致警告:Illegal offset type。
這里有兩種創(chuàng)建關(guān)聯(lián)數(shù)組的方法:
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
or:
$age['Peter']="35";
 $age['Ben']="37";
 $age['Joe']="43"; 

遍歷

<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
foreach($age as $x=>$x_value)
 {
 echo "Key=" . $x . ", Value=" . $x_value;
 echo "<br>";
 }
// 自 PHP 5.4 起
$array = [    "foo" => "bar",    "bar" => "foo",];
?>

5.多維數(shù)組

<?php
// 二維數(shù)組:
$cars = array
 (
     array("Volvo",100,96),
    array("BMW",60,59),
    array("Toyota",110,100)
 );

創(chuàng)建

<?php 
$sites = array 
( 
    "runoob"=>array 
    ( 
        "菜鳥教程", 
        "http://www.runoob.com" 
    ), 
    "google"=>array 
    ( 
        "Google 搜索", 
        "http://www.google.com" 
    ), 
    "taobao"=>array 
    ( 
        "淘寶", 
        "http://www.taobao.com" 
    ) 
); 
echo $sites['runoob'][0] . '地址為:' . $sites['runoob'][1];
?> 

修改數(shù)組

<?php
$arr = array(5 => 1, 12 => 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
?>

注意這里所使用的最大整數(shù)鍵名不一定當(dāng)前就在數(shù)組中。它只要在上次數(shù)組重新生成索引后曾經(jīng)存在過就行了。以下面的例子來說明:

<?php
// 創(chuàng)建一個簡單的數(shù)組
$array = array(1, 2, 3, 4, 5);
print_r($array);

// 現(xiàn)在刪除其中的所有元素,但保持?jǐn)?shù)組本身不變:
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
)

數(shù)組函數(shù)

1.unset()

函數(shù)允許刪除數(shù)組中的某個鍵。但要注意數(shù)組將不會重建索引。如果需要刪除后重建索引,可以用 array_values() 函數(shù)。

<?php
$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');
*/

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

2.foreach 遍歷數(shù)組,引用賦值

<?php
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
    $value = $value * 2;
}
// $arr is now array(2, 4, 6, 8)
unset($value); // 最后取消掉引用
?>

更多例子

<?php
/* foreach example 1: value only */

$a = array(1, 2, 3, 17);

foreach ($a as $v) {
   echo "Current value of \$a: $v.\n";
}

/* foreach example 2: value (with its manual access notation printed for illustration) */

$a = array(1, 2, 3, 17);

$i = 0; /* for illustrative purposes only */

foreach ($a as $v) {
    echo "\$a[$i] => $v.\n";
    $i++;
}

/* foreach example 3: key and value */

$a = array(
    "one" => 1,
    "two" => 2,
    "three" => 3,
    "seventeen" => 17
);

foreach ($a as $k => $v) {
    echo "\$a[$k] => $v.\n";
}

/* foreach example 4: multi-dimensional arrays */
$a = array();
$a[0][0] = "a";
$a[0][1] = "b";
$a[1][0] = "y";
$a[1][1] = "z";

foreach ($a as $v1) {
    foreach ($v1 as $v2) {
        echo "$v2\n";
    }
}

/* foreach example 5: dynamic arrays */

foreach (array(1, 2, 3, 4, 5) as $v) {
    echo "$v\n";
}
?>

3.用 list() 給嵌套的數(shù)組解包

<?php
$array = [
    [1, 2],
    [3, 4],
];

foreach ($array as list($a, $b)) {
    // $a contains the first element of the nested array,
    // and $b contains the second element.
    echo "A: $a; B: $b\n";
}
?>
//output
A: 1; B: 2
A: 3; B: 4

list() 中的單元可以少于嵌套數(shù)組的,此時多出來的數(shù)組單元將被忽略:

<?php
$array = [
    [1, 2],
    [3, 4],
];

foreach ($array as list($a)) {
    // Note that there is no $b here.
    echo "$a\n";
}
?>
//outout
1
3

4.排序

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

5.數(shù)組(Array) 的賦值總是會涉及到值的拷貝。使用引用運算符通過引用來拷貝數(shù)組。

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

7.給數(shù)頭部添加元素:array_unshift();

<?php
 $arr = array(1,2,3,4,5,6,7,8); 
array_unshift($arr,8,9); //數(shù)組前面添加8,9 
print_r($arr);?>

8. 給數(shù)組尾部添加元素:array_push();

<?php 
$arr = array(1,2,3,4,5,6,7,8); 
array_push($arr,8,9); //數(shù)組后面添加
 print_r($arr);
?>

9.定義數(shù)組的長度添加元素:array_pad(array,定義數(shù)組長度,添加的值);

當(dāng)定義數(shù)組長度大于數(shù)組本身長度時,在數(shù)組的尾部添加要添加的元素直至數(shù)組長度為定義的數(shù)組的長度;當(dāng)數(shù)組定義長度等于或者小于數(shù)組本身的長度時,則不作操作;當(dāng)數(shù)組定義的長度是負數(shù)且絕對值大于數(shù)組長度時,則在數(shù)組的頭部添加元素,直至數(shù)組的長度為定義長度的絕對值長。

<?php
 $arr = array(1,2,3,4,5,6,7,8); 
$arr = array_pad($arr,10,10); 
print_r($arr);//輸出數(shù)組
 echo('<br/>'); //創(chuàng)建一個換行符
 $arr = array_pad($arr,-15,-1); 
print_r($arr); //輸出數(shù)組
?>
Paste_Image.png

10.刪除數(shù)組尾部的元素array_pop();

<?php 
$arr = array(1,2,3,4,5,6,7,8); // 返回要刪除的元素,輸出已刪除元素后的數(shù)組
 $a = array_pop($arr); //聲明變量a存放刪除的元素 echo($a); //輸出a 
echo('<br/>'); //創(chuàng)建一個換行符
 print_r($arr); //輸出數(shù)組arr
?>

11.刪除數(shù)組頭部的元素array_shift();

<?php
 $arr = array(1,2,3,4,5,6,7,8);
 // 刪除頭部數(shù)組元素 
$a = array_shift($arr); 
echo($a.'<br/>'); //輸出刪除的元素
 print_r($arr); // 輸出刪除元素后的數(shù)組
?>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容