PHP器皿

WBOY
Release: 2016-06-13 13:19:31
Original
813 people have browsed it

PHP容器
每种语言的容器都不太一样,最起码名字不一样,不过有一个是一样的,就是Array~
Java是我目前见过最多容器类型的语言了~
PHP的弱类型特征,使得容器看起来很OO。


Array

boolean print_r($array); //用来输出数组,还有用while遍历打印
is_array()
Copy after login




create:array、range、list
$state[0] = "0";
$state[1] = "1";
$state[]  = "2";  //数字索引才能这么玩的
echo $state[0].$state[2];
//	长度无需提前定义,动态

$state["key"] = "value";
//	还可以使用字符串做下标,全对象的吗?


$array1 = array("OH" => "Ohio", "CA" => "California", "HI" => "Hawaii");
//	这不就是OC的Dictionary的创建方式么~




//  便利地创建有序数组
//  操作数据库时,听说就是用list来玩,可以一次读取多个对象
Copy after login



常用array函数
boolean in_array($tag, $array    [boolean]);//最后是否匹配类型
boolean array_key_exists(key, $array);


array array_keys($array, [$key]);  //可选参数的意思是?
array array_values($array);
key   array_search($value, $array, [boolean]); //可选,类型匹配?
array echo($array) =>返回当前的key-value

key key($array)   => 类似jdbc读取ResultSet
key next($array)  => 下一个
key prev($array)  => 上一个
key reset($array) => 指针回到开头
key end($array)   => 最后一个
key current($array) => 当前,但不移动指针,简直就和文件操作一个样子~
Copy after login


//  书上说很有用的一个函数,将数据传给函数,特附上例子
boolean array_walk($array, callback function,  [可选传入函数的第三个参数C]);

    //  fuck,函数接口是两个参数,也可以有第三个参数,对应C
    //  想改值就传指针~,PHP也有指针玩耶
    function sanitize_data(&$value, $key) {
        $value = strip_tags($value);
    }

    $ay['keyword'] = "yes";
    array_walk($ay['keyword'],"sanitize_data");

$array1 = array("OH","CA","NY","HI","CT");
$array3 = array("TX","MD","NE","OH","HI");
$intersection = array_intersect($array1, $array3);
print_r($intersection);
Copy after login


//P85,抄个名字~
array_reverse()逆置
array_flip()键值交换
count()
array_count_values()
array_unique()去除重复的值
Copy after login


排序竟然有那么多函数>
sort()
natsort()
natcasesort()
rsort()
asort()
array_multisort()
arsort()
ksort()
krsort()
usort()
Copy after login



//  合并、拆分、接合、分解
array_combine()
array_merge()
array_merge_recursive()
array_slice()
array_splice()
array_intersect()
array_intersect_assoc()
array_diff()
array_diff_assoc



array_rand()
shuffle()
array_sum()array_chunk()
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template