Php数组操作

1.定义数组

 

2.获取数组长度

 

3.遍历数组

";
 }
?> 

4.关联数组,类似map

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

1.包含有合法整型值的字符串会被转换为整型。例如键名 "8" 实际会被储存为 8。
但是 "08" 则不会强制转换,因为其不是一个合法的十进制数值。
2.浮点数也会被转换为整型,意味着其小数部分会被舍去。例如键名 8.7 
实际会被储存为 8。
3.布尔值也会被转换成整型。即键名 true 实际会被储存为 1 而键名 false 会被储存为 0
4.Null 会被转换为空字符串,即键名 null 实际会被储存为 ""。
5.数组和对象不能被用为键名。坚持这么做会导致警告:Illegal offset type。
这里有两种创建关联数组的方法:
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
or:
$age['Peter']="35";
 $age['Ben']="37";
 $age['Joe']="43"; 

遍历

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

5.多维数组

创建

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

修改数组

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

注意这里所使用的最大整数键名不一定当前就在数组中。它只要在上次数组重新生成索引后曾经存在过就行了。以下面的例子来说明:

 $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
)

数组函数

1.unset()

函数允许删除数组中的某个键。但要注意数组将不会重建索引。如果需要删除后重建索引,可以用 array_values() 函数。

 '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 遍历数组,引用赋值


更多例子

 $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() 给嵌套的数组解包


//output
A: 1; B: 2
A: 3; B: 4

list() 中的单元可以少于嵌套数组的,此时多出来的数组单元将被忽略:


//outout
1
3

4.排序


5.数组(Array) 的赋值总是会涉及到值的拷贝。使用引用运算符通过引用来拷贝数组。


7.给数头部添加元素:array_unshift();


8. 给数组尾部添加元素:array_push();


9.定义数组的长度添加元素:array_pad(array,定义数组长度,添加的值);

当定义数组长度大于数组本身长度时,在数组的尾部添加要添加的元素直至数组长度为定义的数组的长度;当数组定义长度等于或者小于数组本身的长度时,则不作操作;当数组定义的长度是负数且绝对值大于数组长度时,则在数组的头部添加元素,直至数组的长度为定义长度的绝对值长。

'); //创建一个换行符
 $arr = array_pad($arr,-15,-1); 
print_r($arr); //输出数组
?>
Paste_Image.png

10.删除数组尾部的元素array_pop();

'); //创建一个换行符
 print_r($arr); //输出数组arr
?>

11.删除数组头部的元素array_shift();

'); //输出删除的元素
 print_r($arr); // 输出删除元素后的数组
?>

你可能感兴趣的:(Php数组操作)