<?php
/**
 * Created by PhpStorm.
 */

//数组处理函数及应用

//range()函数 快速创建一个从start到end范围的数字数组或字符数组
echo "range()函数:<br>";
$numbers1 = range(1,9);   //等价于下面的
$numbers2 = array(1,2,3,4,5,6,7,8,9);
print_r($numbers1); echo "<br>";
print_r($numbers2); echo "<br>"; //这样写代码,不是良好的习惯,代码尽量独占一行

$chars1 = range('a','d'); //等价于下面
$chars2 = array('a','b','c','d');
print_r($chars1); echo "<br>";
print_r($chars2); echo "<br>"; echo "<hr>";

//explode函数 使用指定分隔符,分隔字符串,放置数组中
echo "explode()函数:<br>";
$ip = '192.168.1.101';
$ips = explode('.',$ip);
print_r($ips); echo "<br>"; echo "<hr>";

//array_combine()函数 创建一个新数组,用一个数组作为key,一个数组作为value
echo "array_combine()函数:<br>";
$array_k = array('a','b','c','d');
$array_v = array('apple','banana','cherry','Durian');
$array_new = array_combine($array_k,$array_v);
print_r($array_k); echo "<br>";
print_r($array_v); echo "<br>";
print_r($array_new); echo "<br>"; echo "<hr>";


//array_fill()函数 创建一个数组,数组长度length,元素从start_key开始递增
echo "array_fill()函数:<br>";
$fill1 = array_fill(2,3,'虾米大王');
print_r($fill1); echo "<br>";
$fill2 = array_fill(2,-1,'虾米大王'); //长度为负数,错误
print_r($fill2); echo "<br>"; echo "<hr>";

//array_pad()函数 返回数组的一个拷贝,并用pad_value填补到pad_size指定的长度
//pad_size为正数,填补到右侧,pad_size为负数,从左侧填补,pad_size小于数组长度,不填补
echo "array_pad()函数:<br>";
$info = array('one','two','threee');
$tmp1 = array_pad($info,5,'xx');
$tmp2 = array_pad($info,-5,'xx');
$tmp3 = array_pad($info,2,'xx');
print_r($tmp1); echo "<br>";
print_r($tmp2); echo "<br>";
print_r($tmp3); echo "<br>"; echo "<hr>";

//count()函数 统计数组中元素个数,多维数组mode参数COUNT_RECURSIVE,mode默认为0;
echo "count()函数:<br>";
$arr1 = array('a'=>'red','b'=>'green',2,5);
$arr2 = array(
    "001"=> array('NO'=>'001','name'=>'zhang','sex'=>'male'),
    "002"=> array('NO'=>'002','name'=>'wang','sex'=>'female')
);
$count1 = count($arr1);
$count2 = count($arr2,COUNT_RECURSIVE); //
var_dump($count1); echo "<br>";
var_dump($count2); echo "<br>"; echo "<hr>";


//max()函数 统计数组中元素最大值
echo "max()函数:<br>";
$scores = array(70,80,90,60);
$grades = array('A','B','C','D');
$max1 = max($scores);
$max2 = max($grades);
var_dump($max1); echo "<br>"; //90
var_dump($max2); echo "<br>"; echo "<hr>"; //D


//array_sum函数() 统计数组元素的和,返回整数或浮点数
echo "array_sum()函数:<br>";
$scores = array(70,80,90,60);
$grades = array('1A','2B','3C','4D'); //字符省略,
$sum1 = array_sum($scores);
$sum2 = array_sum($grades);
var_dump($sum1); echo "<br>";
var_dump($sum2); echo "<br>"; echo "<hr>";


//array_product()函数 统计数组中元素的乘积,返回整数或浮点数
echo "array_product()函数:<br>";
$arr1 = array(7,8,9,6);
$arr2 = array('1a','2b','3c','4d');
$p1 = array_product($arr1);
$p2 = array_product($arr2);
var_dump($p1); echo "<br>";
var_dump($p2); echo "<br>"; echo "<hr>";

//array_count_values()函数 统计数组中元素值出现的次数
$arr1 = array(1,'hello',1,'world','hello');
print_r(array_count_values($arr1)); echo "<br>"; echo "<hr>"; //


//key()函数 返回数组当前指针的key名
//current()函数 返回数组当前指针的value值,别名pos
echo "key()函数:<br>";
echo "current()函数:<br>";
$examples[1] = "beijing";
$examples[5] = 'ningbo';
$examples[2] = 'hangzhou';
$examples[] = 'xian';
var_dump(key($examples)); echo "<br>";  //当前
var_dump(current($examples)); echo "<br>"; echo "<hr>";

//next()函数 移动数组当前指针至下一个元素
echo "next()函数:<br>";
$examples[1] = "beijing";
$examples[5] = 'ningbo';
$examples[2] = 'hangzhou';
$examples[] = 'xian';
$second = next($examples);
$third = next($examples);
var_dump(key($examples)); echo "<br>";
var_dump(current($examples)); echo "<br>"; //输出hangzhou
var_dump($second); echo "<br>"; //输出ningbo
var_dump($third); echo "<br>"; echo "<hr>";//输出hangzhou

//end()函数 移动数组当前指针至最后一个元素
echo "end()函数:<br>";
$examples[1] = "beijing";
$examples[5] = 'ningbo';
$examples[2] = 'hangzhou';
$examples[] = 'xian';
$end = end($examples);
var_dump(key($examples)); echo "<br>";
var_dump(current($examples)); echo "<br>";
var_dump($end); echo "<br>"; echo "<hr>";

//prev()函数 移动数组当前指针至上一个元素,并返回当前指针的值
echo "prev()函数:<br>";
$arr[1] = "beijing";
$arr[5] = 'ningbo';
$arr[2] = 'hangzhou';
$arr[] = 'xian';
$end = end($arr);
$prev = prev($arr);
var_dump(key($arr)); echo "<br>";
var_dump(current($arr)); echo "<br>"; //输出hangzhou
var_dump($end); echo "<br>"; //输出xian
var_dump($prev); echo "<br>"; echo "<hr>";//输出hangzhou

//reset()函数 移动数组当前指针至第一个元素
echo "reset()函数:<br>";
$examples[1] = "beijing";
$examples[5] = 'ningbo';
$examples[2] = 'hangzhou';
$examples[] = 'xian';
$end = end($examples);
$first = reset($examples);
var_dump(key($examples)); echo "<br>";
var_dump(current($examples)); echo "<br>"; //输出beijing
var_dump($end); echo "<br>"; //输出xian
var_dump($first); echo "<br>"; echo "<hr>"; //输出beijing

//each()函数 以数组形式返回当前指针的元素key和value,指针后移
$examples[1] = "beijing";
$examples[5] = 'ningbo';
$examples[2] = 'hangzhou';
$examples[] = 'xian';
$each = each($examples);
print_r($each); echo "<br>";
var_dump(key($examples)); echo "<br>";
var_dump(current($examples)); echo "<br>"; echo "<hr>";


//数组的遍历
$array_k = array('a','b','c','d');
$array_v = array('apple','banana','cherry','Durian');
$array_new = array_combine($array_k,$array_v);
reset($array_new);
do
{
    $k = key($array_new);
    $v = current($array_new);
    echo "$k => $v"."<br>";
}while(next($array_new)); echo "<br>"; echo "<hr>";

//如果数组中有空值,使用next遍历会出现失败
$arr1 = range('a','e');
$arr1[5] = 0;
print_r($arr1);
echo "<br>";
reset($arr1);
do
{
    $k = key($arr1);
    $v = current($arr1);
    echo "$k => $v"."<br>";
}while(next($arr1)); echo "<br>"; echo "<hr>";


//list()语言结构 仅用于数字键key的数组
echo "list()语言结构:<br>";
$info = array('china','gansu','lanzhou');
list($country,$province,$city) = $info;
echo "we are come from $country-$province-$city."."<br>";
list($country,,$city) = $info;
echo "we are come from $country,$city"."<br>";
list(,,$city) = $info;
echo "we are come from $city"."<br>"; echo "<hr>";


//extract()函数 使用数组定义一组变量
echo "extract()函数:<br>";
$info = array('student_no'=>'001','student_name'=>'wang','student_sex'=>'male');
extract($info);
echo "student_no = $student_no"."<br>";
echo "student_name = $student_name"."<br>";
echo "student_sex = $student_sex"."<br>"; echo "<hr>";


//compact()函数 返回一个数组
echo "compact()函数:<br>";
$tel = '130****1011';
$email = 'name@xxx.com';
$postcode = '730900';
$resutl = compact('tel','email','postcode');
print_r($resutl);
echo "<br>"; echo "<hr>";

//数组遍历
echo "数组遍历:<br>";
$array_k = array('a','b','c','d');
$array_v = array('apple','banana','cherry','Durian');
$array_new = array_combine($array_k,$array_v);
reset($array_new);
while(list($k,$v) = each($array_new))
{
    echo "$k => $v"."<br>";
}
echo "<br>"; echo "<hr>";

//foreach数组遍历
echo "foreach数组遍历<br>";
unset($examples); //因为examples[] = 'xian' ,同名变量添加,造成数组不准确,需要先清空
$examples[1] = "beijing";
$examples[5] = 'ningbo';
$examples[2] = 'hangzhou';
$examples[] = 'xian';
foreach ($examples as $value)
{
    echo $value. " - ";
}
echo "<br>"; echo "<hr>";

//foreach数组遍历
echo "foreach数组遍历<br>";
unset($examples); //因为examples[] = 'xian' ,同名变量添加,造成数组不准确,需要先清空
$examples[1] = "beijing";
$examples[5] = 'ningbo';
$examples[2] = 'hangzhou';
$examples[] = 'xian';
print_r($examples); echo "<br>";
foreach ($examples as $key =>$value)
{
    echo "you come from [$key]$value"."<br>";
}
echo "<hr>";

//array_keys()函数  以数组形式返回key
echo "array_keys()函数:<br>";
unset($examples); //因为examples[] = 'xian' ,同名变量添加,造成数组不准确,需要先清空
$examples[1] = "beijing";
$examples[5] = 'ningbo';
$examples[2] = 'hangzhou';
$examples[] = 'xian';
$keys = array_keys($examples);
print_r($keys); echo "<br>";
$keys = array_keys($examples,'hangzhou');
print_r($keys); echo "<br>"; echo "<hr>";

//array_values()函数 以数组形式返回元素值
echo "array_values()函数:<br>";
unset($examples); //因为examples[] = 'xian' ,同名变量添加,造成数组不准确,需要先清空
$examples[1] = "beijing";
$examples[5] = 'ningbo';
$examples[2] = 'hangzhou';
$examples[] = 'xian';
$values = array_values($examples);
print_r($values); echo "<br>"; echo "<hr>";

//in_array()函数 检查数组中是否存在searchvalue,参数strict为true,还会检查类型是否相同
echo "in_array()函数:<br>";
$words = array('JAVA','PHP','DOTNET');
$java1 = in_array('java',$words);
$php = in_array('PHP',$words);
var_dump($java1); echo "<br>"; //false
var_dump($php); echo "<br>"; //true
$numbers3 = array('1.10',12.1,1.15);
$n1 = in_array(1.10,$numbers3);
$n2 = in_array(1.10,$numbers3,true);
var_dump($n1); echo "<br>"; //true
var_dump($n2); echo "<br>"; echo "<hr>"; //false

//array_key_exists()函数 检查数组中是否存在key,
echo "array_key_exists()函数:<br>";
$words = array('SUN'=>'JAVA','Microsoft'=>'DOTNET');
$key1 = array_key_exists('SUN',$words);
$key2 = array_key_exists('sun',$words);
var_dump($key1); echo "<br>"; //true
var_dump($key2); echo "<br>"; echo "<hr>"; //false

//array_search()函数 在数组中搜索searchvalue,参数strict为true,还会检查类型是否相同
echo "array_search()函数:<br>";
$words = array('DOTNET'=>'Microsoft','JAVA'=>'SUN','JSP'=>'SUN');
$key1 = array_search('sun',$words);
$key2 = array_search('Microsoft',$words);
var_dump($key1); echo "<br>";  //false
var_dump($key2); echo "<br>";  //返回键名
$numbers4 = array('PI'=>'3.14','直角'=>'90');
$key3 = array_search(90,$numbers4);
$key4 = array_search(90,$numbers4,true);
var_dump($key3); echo "<br>";  //返回键名
var_dump($key4); echo "<br>"; echo "<hr>"; //false

//array_unique()函数 返回一个移除重复元素的新数组
echo "array_unique()函数:<br>";
$colors = array('a'=>'green','red','b'=>'green','blue','red');
$colors_unique = array_unique($colors);
print_r($colors_unique); echo "<br>";
$inputs = array(4,'4','3',3,4,'3');
$inputs_u = array_unique($inputs);
print_r($inputs_u); echo "<hr>";

//sort()函数 根据值升序排列,重新赋值键名
echo "sort()函数:<br>";
$images = array('a'=>'img12.png','b'=>'img10.png','c'=>'img02.png','d'=>'img01.png');
print_r($images); echo "<br>";
sort($images);
print_r($images); echo "<hr>";

//asort()函数 根据值升序排列,保留键名
echo "asort()函数:<br>";
$images = array('a'=>'img12.png','b'=>'img10.png','c'=>'img02.png','d'=>'img01.png');
print_r($images); echo "<br>";
asort($images);
print_r($images); echo "<hr>";

//rsort()函数 根据值降序排列,重新赋值键名
echo "rsort()函数:<br>";
$images = array('a'=>'img10.png','b'=>'img12.png','c'=>'img01.png','d'=>'img02.png');
print_r($images); echo "<br>";
rsort($images);
print_r($images); echo "<hr>";

//arsort()函数 根据值降序排列,重新赋值键名
echo "arsort()函数:<br>";
$images = array('a'=>'img10.png','b'=>'img12.png','c'=>'img01.png','d'=>'img02.png');
print_r($images); echo "<br>";
arsort($images);
print_r($images); echo "<hr>";

//ksort()函数 根据数组keyname升序排序,保持原有键值对应关系
echo "ksort()函数:<br>";
$country3 = array('c'=>'China','f'=>'French','e'=>'English');
print_r($country3); echo "<br>";
ksort($country3);
print_r($country3); echo "<hr>";

//krsort()函数 根据数组keyname降序排序,保持原有键值对应关系
echo "krsort()函数:<br>";
$country4 = array('c'=>'China','f'=>'French','e'=>'English');
print_r($country4); echo "<br>";
krsort($country4);
print_r($country4); echo "<hr>";

//natsort()函数 用自然排序对元素值区分大小写升序排序,保持原有键值对应关系
echo "natsort()函数:<br>";
$images = array('dog1'=>'IMG10.jpg','dog2'=>'img13.jpg','dog3'=>'IMG04.jpg','dog4'=>'img07.jpg');
print_r($images); echo "<br>";
natsort($images);
print_r($images); echo "<hr>";


//natcasesort()函数 用自然排序对元素值不区分大小写升序排序,保持原有键值对应关系
echo "natcasesort()函数:<br>";
$images = array('dog1'=>'IMG10.jpg','dog2'=>'img13.jpg','dog3'=>'IMG04.jpg','dog4'=>'img07.jpg');
print_r($images); echo "<br>";
natcasesort($images);
print_r($images); echo "<hr>";

//shuffle()函数 为数组随机排序
echo "shuffle()函数:<br>";
$images = array('img01','img03','img04','img02','img06','img05');
print_r($images); echo "<br>";
shuffle($images);
print_r($images); echo "<hr>";

//array_reverse()函数  返回一个和原数组顺序相反的新数组,参数preserve_keys为true保持对应
echo "array_reverse()函数:<br>";
$images = array('1'=>'dog1','4'=>'dog4','2'=>'dog2','3'=>'dog3');
print_r($images); echo "<br>";
$rev1 = array_reverse($images);
$rev2 = array_reverse($images,true);
print_r($rev1); echo "<br>";
print_r($rev2); echo "<hr>"; //键为整数有效,字符无效

//array_push()函数 把数组当做栈,依次压入新元素,返回数组元素个数
echo "array_push()函数:<br>";
$stack = range('a','d');
$counts = array_push($stack,'AA','BB');
echo "stack = $counts<br>";
print_r($stack); echo "<hr>";

//array_pop()函数 弹出数组最后一个元素,并返回该元素值。
echo "array_pop()函数:<br>";
$stack = range('a','d');
$c = array_pop($stack);
echo "pop = $c"."<br>";
print_r($stack); echo "<hr>";


//array_unshift()函数 依次插入数组队首,返回入队元素个数
echo "array_unshift()函数:<br>";
$queue = array('orange','banana');
print_r($queue); echo "<br>";
$counts = array_unshift($queue,'pear');
echo $counts; echo "<br>";
print_r($queue); echo "<br>";
$counts = array_unshift($queue,'apple');
echo $counts; echo "<br>";
print_r($queue); echo "<hr>";

//array_shift()函数 依次删除数组队首,返回元素个数
echo "array_shift()函数:<br>";
$queue = array('orange','banana','apple','pear');
print_r($queue); echo "<br>";
$shift = array_shift($queue);
echo "shift = $shift"."<br>";
print_r($queue); echo "<hr>";

//array_merge()函数 集合的并集运算
echo "array_merge()函数:<br>";
$arr1 = array('color'=>'red','green','blue','num'=>2);
$arr2 = array('a','b','color'=>'green','shape'=>'circle','num'=>4);
$result1 = array_merge($arr1,$arr2);
print_r($arr1); echo "<br>";
print_r($arr2); echo "<br>";
print_r($result1); echo "<hr>";


//array_diff()函数 集合的差集运算,array1为主
echo "array_diff()函数:<br>";
$arr3 = array('a'=>'green','red','blue','red');
$arr4 = array('b'=>'green','red','yellow');
$result2 = array_diff($arr3,$arr4);
print_r($arr3); echo "<br>";
print_r($arr4); echo "<br>";
print_r($result2); echo "<hr>";


//array_intersect()函数 集合的交集运算
echo "array_intersect()函数:<br>";
$arr5 = array('a'=>'green','red','blue');
$arr6 = array('b'=>'green','red','yellow');
$result3 = array_intersect($arr5,$arr6);
print_r($arr5); echo "<br>";
print_r($arr6); echo "<br>";
print_r($result3); echo "<hr>";

//array_diff_assoc()函数 集合的差集运算,键名也比较
echo "array_diff_assoc()函数:<br>";
$arr3 = array('a'=>'green','red','blue','red');
$arr4 = array('b'=>'green','red','yellow');
$result2 = array_diff_assoc($arr3,$arr4);
print_r($arr3); echo "<br>";
print_r($arr4); echo "<br>";
print_r($result2); echo "<hr>";

//array_intersect_assoc()函数 集合的交集运算,键名也比较
echo "array_intersect_assoc()函数:<br>";
$array1 = array('a'=>'green','red','blue');
$array2 = array('b'=>'green','red','yellow');
$result4 = array_intersect_assoc($array1,$array2);
print_r($array1); echo "<br>";
print_r($array2); echo "<br>";
print_r($result4); echo "<hr>";

//array_diff_key()函数 集合的差集运算,array1为主
echo "array_diff_key()函数:<br>";
$array1 = array('blue'=>1,'red'=>2,'green'=>3,'white'=>4);
$array2 = array('green'=>5,'blue'=>6,'yellow'=>7,'black'=>8);
$result = array_diff_key($array1,$array2);
print_r($array1); echo "<br>";
print_r($array2); echo "<br>";
print_r($result); echo "<hr>";

//array_intersect_key()函数 集合的交集运算,键比较
echo "array_intersect_key()函数:<br>";
$array1 = array('blue'=>1,'red'=>2,'green'=>3,'white'=>4);
$array2 = array('green'=>5,'blue'=>6,'yellow'=>7,'black'=>8);
$result = array_intersect_key($array1,$array2);
print_r($array1); echo "<br>";
print_r($array2); echo "<br>";
print_r($result); echo "<hr>";