《PHP Cookbook》学习笔记(二)

四、数组

1、array_push():会把一个新值推送到数组栈的顶部。
添加新值可以用$foo[]标识符,是PHP更为传统的风格,而且速度也更快。

2、array_pop():用于删除数组中的最后一个元素,并返回这个元素。

3、array_map():将回调函数作用到给定数组的单元上。
(1)适合处理并合并多个数组,例如:
<?php function show_Spanish($n, $m) { return("The number $n is called $m in Spanish"); } function map_Spanish($n, $m) { return(array($n => $m)); } $a = array(1, 2, 3, 4, 5); $b = array("uno", "dos", "tres", "cuatro", "cinco"); $c = array_map("show_Spanish", $a, $b); print_r($c); $d = array_map("map_Spanish", $a , $b); print_r($d); ?>
上例输出:
// printout of $c
Array
(
    [0] => The number 1 is called uno in Spanish
    [1] => The number 2 is called dos in Spanish
    [2] => The number 3 is called tres in Spanish
    [3] => The number 4 is called cuatro in Spanish
    [4] => The number 5 is called cinco in Spanish
)

// printout of $d
Array
(
    [0] => Array
        (
            [1] => uno
        )

    [1] => Array
        (
            [2] => dos
        )

    [2] => Array
        (
            [3] => tres
        )

    [3] => Array
        (
            [4] => cuatro
        )

    [4] => Array
        (
            [5] => cinco
        )

)
(2)本函数一个有趣的用法是构造一个数组的数组,这可以很容易的通过NULL作为回调函数名来实现,例如:
<?php $a = array(1, 2, 3, 4, 5); $b = array("one", "two", "three", "four", "five"); $c = array("uno", "dos", "tres", "cuatro", "cinco"); $d = array_map(null, $a, $b, $c); print_r($d); ?>
上例输出:
Array
(
    [0] => Array
        (
            [0] => 1
            [1] => one
            [2] => uno
        )

    [1] => Array
        ( 
            [0] => 2
            [1] => two
            [2] => dos
        )

    [2] => Array
        (
            [0] => 3
            [1] => three
            [2] => tres
        )

    [3] => Array
        (
            [0] => 4
            [1] => four
            [2] => cuatro
        )

    [4] => Array
        (
            [0] => 5
            [1] => five
            [2] => cinco
        )

)

4、settype():设置变量的类型。
<?php settype( $items, 'array' ); //强制将变量$items转换成数组 settype( $items, 'integer' ); //强制将变量$items转换成整数 settype( $items, 'string' ); //强制将变量$items转换成字符串 ?>

5、array_splice():自动地重新索引数组以清除空缺的元素位置。

6、array_pad():用值将数组填补到指定的长度,这个函数会返回修改后的数组,但不会修改原来的数组。
<?php $array = array('apple', 'banana', 'coconut'); $array = array_pad($array, 4, 'dates'); //第一个参数是要扩充的数组;第二个参数表示要扩充的大小和方向,正数向右扩充,负数向左扩充;第三个参数是要赋给新创建元素的值。 print_r($array); Array ( [0] => apple [1] => banana [2] => coconut [3] => dates ) ?>

7、array_merge():合并一个或多个数组。
(1)为了避免冲突,大写字母A的索引经过重新编号,由0变成了1,并被添加到了新数组的末尾,同时新数组中大写字母B则覆盖了小写字母b并取代其在数组中的原始位置。
<?php $lc = array('a', 'b' => 'b'); // lower-case letters as values $uc = array('A', 'b' => 'B'); // upper-case letters as values $ac = array_merge($lc, $uc); // all-cases? print_r($ac); Array ( [0] => a [b] => B [1] => A ) ?>
(2)使用+操作符也可以实现合并数组的功能,此时右边的数组会覆盖左边的数组中所有重名的键,而且也不会为避免冲突而进行索引重排。
<?php print_r($a + $b); print_r($b + $a); Array ( [0] => a [b] => b ) Array ( [0] => A [b] => B ) ?>

8、array_search():在数组中搜索给定的值,如果成功则返回相应的键名,否则返回false。
使用此函数应注意,此函数可能会返回0键,所以应用===false和!==false检查。

9、sort():对数组进行排序。
<?php $scores = array(1, 10, 2, 20); sort($scores, SORT_NUMERIC); //第二个参数会按数字升序,而不是字典顺序排序,所以结果为(1, 2, 10, 20),而不是(1, 10, 2, 20) ?>

10、natsort():对数组按自然的排序选法进行排序。
<?php $tests = array('test1.php', 'test10.php', 'test11.php', 'test2.php'); natsort($tests); //排序后'test1.php', 'test2.php', 'test10.php', and 'test11.php'. ?>

11、natcasersort():区分大小写的自然排序。

12、array_multisort():对多个数组或者多维数组进行排序
<?php $colors = array('Red', 'White', 'Blue'); $cities = array('Boston', 'New York', 'Chicago'); array_multisort($colors, $cities); print_r($colors); print_r($cities); Array ( [0] => Blue [1] => Red [2] => White ) Array ( [0] => Chicago [1] => Boston [2] => New York ) ?>
此函数可以像sort()一样传递参数,参数列表如下:
(1)排序顺序标志:
SORT_ASC - 按照上升顺序排序
SORT_DESC - 按照下降顺序排序
(2)排序类型标志:
SORT_REGULAR - 将项目按照通常方法比较
SORT_NUMERIC - 将项目按照数值比较
SORT_STRING - 将项目按照字符串比较

五、变量

貌似没啥可说滴,注意基础吧,不信自己去看《PHP Cookbook》

六、函数

1、虽然在PHP中传递引用的速度会快些,但与传递值相比也所差无几,为此,我们建议只在确实必要的情况下传递变量的引用,并且任何时候也不要将其作为提升性能的一种诀窍来运用。

 

你可能感兴趣的:(《PHP Cookbook》学习笔记(二))