昶萌

每日五个PHP函数(3)
array_keys — 返回数组中部分的或所有的键名// 说明:array_keys ( array $arra...
扫描右侧二维码阅读全文
23
2019/07

每日五个PHP函数(3)

array_keys — 返回数组中部分的或所有的键名

// 说明:array_keys ( array $array [, mixed $search_value = null [, bool $strict = false ]] ) : array

$array = array(0 => 100, "color" => "red");
print_r(array_keys($array));

$array = array("blue", "red", "green", "blue", "blue");
print_r(array_keys($array, "blue"));

$array = array("color" => array("blue", "red", "green"),
               "size"  => array("small", "medium", "large"));
print_r(array_keys($array));

//  输出:
Array
(
    [0] => 0
    [1] => color
)
Array
(
    [0] => 0
    [1] => 3
    [2] => 4
)
Array
(
    [0] => color
    [1] => size
)

array_values — 返回数组中所有的值

// 说明:array_values ( array $array ) : array

$array = array("size" => "XL", "color" => "gold");
print_r(array_values($array));

// 输出:
Array
(
    [0] => XL
    [1] => gold
)

array_merge — 合并一个或多个数组

// 说明:array_merge ( array $array1 [, array $... ] ) : array
// 如果数组中有相同的字符串键名,则该键名后面的值覆盖前面的值;如果想让前面的值覆盖后面,则可以使用 + 号。

$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);

$result = array_merge($array1, $array2);
print_r($result);

// 输出:
Array
(
    [color] => green
    [0] => 2
    [1] => 4
    [2] => a
    [3] => b
    [shape] => trapezoid
    [4] => 4
)

str_shuffle — 随机打乱一个字符串

// 说明:str_shuffle ( string $str ) : string

$str = 'abcdef';
$shuffled = str_shuffle($str);

echo $shuffled; // 输出:bfdaec

shuffle — 打乱数组

// 说明:shuffle ( array &$array ) : bool

$numbers = range(1, 20);

shuffle($numbers);

foreach ($numbers as $number) {
    echo "$number ";
}

// 输出:7 8 11 1 17 3 14 2 16 15 5 9 4 18 12 20 13 10 19 6 
Last modification:July 23rd, 2019 at 11:30 am

Leave a Comment